Only allow one property to have a given value if another property has another given value

36 views Asked by At

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

There are 1 answers

2
Viliam Popovec On

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.