I would like to validate JSON objects that were parsed into Python dictionaries like the following:
# example with 2 elements
{
'coordinates': [-20.3, 30.6]
}
# example with 3 elements
{
'coordinates': [-20.3, 30.6, 0]
}
So far I was able to define the following schema:
schema = {
'coordinates': {
'required': True,
'type': 'list',
'minlength': 2,
'maxlength': 3,
'schema': {
'type': 'float',
},
}
}
I would also like to check these constraints:
coordinates
field's value should be between -30.0 and 10.0But I was not able to come up with something useful. Does anyone have suggestions how to achieve this?
Update: Based on accepted answer the schema becomes the following
schema = {
'coordinates': {
'required': True,
'type': 'list',
"oneof_items": (
({"min": -30.0, "max": 10.0}, {"min": -10.0, "max": 50.0}),
({"min": -30.0, "max": 10.0}, {"min": -10.0, "max": 50.0}, {}),
),
}
}
docs: https://docs.python-cerberus.org/en/stable/validation-rules.html#of-rules-typesaver
Add this rule:
{"oneof_items":
(
({"min": -30.0, "max": 10.0}, {"min": -10.0, "max": 50.0}),
({"min": -30.0, "max": 10.0}, {"min": -10.0, "max": 50.0}, {}),
)
}
That makes the length-related rules superfluous. To get rid of the redundancies Python object references or the rule set registry are feasible.