Consider the following schema
schema = {
"value_type":{
"type": "string", "required": True
},
"units": {
"type": "string",
"dependencies": {"value_type": ["float", "integer"]},
"required": True
}
}
I want the units field to be required only when the value of value_type field is either float or integer.
Here's the behaviour I aim to achieve
v = Validator(schema)
v.validate({"value_type": "float", "units": "mm"}) # 1.
True
v.validate({"value_type": "boolean", "units": "mm"}) # 2.
False
v.validate({"value_type": "float"}) # 3.
False
v.validate({"value_type": "boolean"}) # 4.
True
The above Schema returns the expected result only for the first 3 cases.
If I change the definition of units (by omitting the "required": True) to
"units": {"type": "string", "dependencies": {"value_type": ["float", "integer"]}}
then the validation of
v.validate({"value_type": "float"}) # 3.
True
returns True which is not what I want.
I took a look at oneof rules in the documentation but couldn't find a way to apply this only to the required property.
I want the value of required to be True only when the dependency is met.
How should I modify my schema to achieve this?