0

I want to do something fairly simple. I've browsed through the questions already asked here but couldn't find an answer to my specific issue. I've played around with excludes and allowed and dependencies, to no avail. Can someone please put me on the right track?

Here's what I'm trying to do:

I have data that looks like {"field1": 1, "field2": true}. I want to allow field2 to be true only if field1 is equal to 1.

Here are a few examples:

Not allowed:

  1. {"field1": 2, "field2" true}

Allowed:

  1. {"field1": 2, "field2" false}
  2. {"field1": 1, "field2": true}

I've tried the following but this doesn't catch the example that is not allowed from above.

'field1': {
        'type': 'integer',
        'default': 1,
},
'field2': {
        'type': 'boolean',
        'default': False,
        'oneof': [{'excludes': 'field1', 'allowed': [1]}, {'allowed': [True]}]
}

1 Answers1

0

You can define allowed key as such: allowed = True if (d['field1'] == 1 and d['field2']) or (d['field1'] != 1 and not d['field2']) else False.

  • this feels like cheating my way out of understanding how cerberus really works. i would prefer to find a way to write it without using python code in the middle of the validation config. – Yohann Pitrey Mar 03 '23 at 11:27
  • Well it's pure python, I am not familiar with cerberus... – Viliam Popovec Mar 04 '23 at 12:07