I want to set conditional validate for empty parameter.
For example:
schema = {
'param1': {'type': 'string', empty: False, required:True, 'allowed': ['One', 'Two']},
'param2': {'type': 'string', empty: False, required:True}
}
I need that empty parameter in param2 determined by the condition:
If param1 == 'One' =>> empty in param2 = True
else False
I tried like that:
'param2': {'type': 'string', empty: {'if': {'param1': 'One'}, 'then': True, 'else': False}, required:True}
But get error: [{'empty': ['must be of boolean type']}]
Of course, it because empty wait for boolean type.
But if there are some solution for this option?
It's not exactly what you want, but the only interactions between fields that cerberus allows are
excludesanddependencies. You can imitate an if statement such asif value == "One" then exclude "param2" else require "param2"like this:Logically the schema translates to
given: p <- param1 has value of "One", q <- exclude param2, r <- require param2
then: (p and q) xor (not p and r) <=> if p then q else r