我想验证 a dict
,其中字段可能包含 anint
或 a List[int]
。此外,一切都int
必须是积极的。
我需要一些帮助来设置架构。下面的架构无法正常工作。他们不检查负数。也就是说,负数正在通过验证,这是不正确的。
import cerberus
v = cerberus.Validator()
schema1 = {
"int_or_list_of_int": {
"type": ["integer", "list"],
"schema": {"type": "integer", "min": 0},
},
}
schema2 = {
"int_or_list_of_int": {
"type": ["integer", "list"],
"valuesrules": {"type": "integer", "min": 0},
},
}
num1 = {"int_or_list_of_int": 5}
num2 = {"int_or_list_of_int": [5, 10]}
num3 = {"int_or_list_of_int": -5}
num4 = {"int_or_list_of_int": [5, -10]}
# schema 1
assert v.validate(num1, schema1)
assert v.validate(num2, schema1)
assert not v.validate(num3, schema1) # Evaluates to True
assert not v.validate(num3, schema1) # Evaluates to True
# schema 2
assert v.validate(num1, schema2)
assert v.validate(num2, schema2)to True
assert not v.validate(num3, schema2) # Evaluates to True
assert not v.validate(num4, schema2) # Evaluates to True