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 excludes
and dependencies
. You can imitate an if statement such as if value == "One" then exclude "param2" else require "param2"
like this:
"param1": {
"allowed": ["One", "Two"],
"oneof": [
{
"allowed": ["One"], # condition
"excludes": ["param2"] # then rule
},
{
"forbidden": ["One"], # inverse condition
"dependencies": ["param2"] # else rule
}
]
}
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.