Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Help: How to validate a dict with at least one of a set of keys? #258

Open
cuteufo opened this issue Apr 13, 2021 · 5 comments
Open

Help: How to validate a dict with at least one of a set of keys? #258

cuteufo opened this issue Apr 13, 2021 · 5 comments

Comments

@cuteufo
Copy link

cuteufo commented Apr 13, 2021

I have a not so ideal validator like:

sch = Schema({
    'a': str,
    'b': int,
    Option('c'): str,
    Option('d'): int
}

in which, a and b must exist, while c or d may or may not exist. It will be fine if neither c nor d shows up. And please also note that c and d's value are of different type.

How can I validate a dict that a and b must exist, while at least one of c and d should exist? I thought of replacing the two Option()s to Regex("c|d"), but was not able to specify the data type according its key value.

Any advice is appreciated.

@HansBambel
Copy link

I want to do something similar. I would like to validate that I have at least one entry:

at_least_one = Schema(
    {
        Optional("a"): Use(int),
        Optional("b"): Use(int),
        Optional("c"): Use(int),
        Optional("d"): Use(int),
        Optional("e"): Use(int),
        Optional("f"): Use(int),
    }
)

Therefore, I want a Schema that renders at_least_one.is_valid({}) to false, but at_least_one.is_valid({"a": 1}) to true.

@harismuzaffer
Copy link

I'm facing the same issue. Anyone able to solve this?

@tivaliy
Copy link

tivaliy commented Jul 27, 2021

Ok, here is what you can try to use:

schema = Schema(
    And({
        # First name
        Optional('foo'): str,
        # Second name
        Optional('bar'): str
    }, lambda d: bool(d))
)
schema.validate({})
...
>> SchemaError: <lambda>({}) should evaluate to True

@harismuzaffer
Copy link

harismuzaffer commented Jul 27, 2021

Ok, here is what you can try to use:

schema = Schema(
    And({
        # First name
        Optional('foo'): str,
        # Second name
        Optional('bar'): str
    }, lambda d: bool(d))
)
schema.validate({})
...
>> SchemaError: <lambda>({}) should evaluate to True

This is helpful but doesn't solve the problem. Apart from foo and bar, if we have a required key in the dict, this won't work

E.g I want to validate {"name": "haris", "foo": "foo value"} such thatname is mandatory whileas out of foo and bar one has to be there, then how would you modify the schema?

@tivaliy
Copy link

tivaliy commented Jul 28, 2021

Ok, here is what you can try to use:

schema = Schema(
    And({
        # First name
        Optional('foo'): str,
        # Second name
        Optional('bar'): str
    }, lambda d: bool(d))
)
schema.validate({})
...
>> SchemaError: <lambda>({}) should evaluate to True

This is helpful but doesn't solve the problem. Apart from foo and bar, if we have a required key in the dict, this won't work

E.g I want to validate {"name": "haris", "foo": "foo value"} such thatname is mandatory whileas out of foo and bar one has to be there, then how would you modify the schema?

How about this one:

In [34]: schema = Schema(
    ...:     And({
    ...:         'required': str,
    ...:         Optional('foo'): str,
    ...:         Optional('bar'): str
    ...:     }, lambda d: any(k in d for k in ('foo', 'bar')), error="Along with required attributes at least 'foo' or 'bar' must be specified"),
    ...: )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants