'dependencies' incorrectly validated by not-required field
See original GitHub issuefrom cerberus import Validator
schema = {
'feature': {
'type': 'dict',
'default_setter': lambda _: {},
'required': False,
'schema': {
'enabled': {
'type': 'boolean',
'required': False,
'default': False
},
'subfeature': {
'required': False,
'type': 'integer',
'nullable': True,
'default': None,
'dependencies': {'^feature.enabled' : True}
}
}
},
}
v = Validator()
v.validate({'feature' : {'enabled' : True}}, schema) # expected True, actual True
v.validate({'feature' : {'enabled' : False}}, schema) # expected True, actual False
v.validate({'feature' : {'enabled' : False, 'subfeature' : None}}, schema) # expected True, actual False
v.validate({'feature' : {'enabled' : True, 'subfeature' : 3}}, schema) # expected True, actual True
v.validate({'feature' : {'enabled' : False, 'subfeature' : 3}}, schema) # expected True, actual False
Used Cerberus version / latest commit:1.3.2 (from pip)
-
I have the capacity to improve the docs when my problem is solved.
-
I have the capacity to submit a patch when a bug is identified.
-
My question does not concern a practical use-case that I can’t figure out to solve.
Use-case abstract
I have a schema in which the main dict named ‘feature’ is optional and has a optional boolean field named “enabled”. This dict has a nested dict named ‘subfeature’ which is also optional and depends on the parent dict’s ‘enabled’ field to be ‘True’ as a validation. Meaning the subfeature cant be set if the feature is not.
Bug report / Feature request
The problem is that when the main ‘feature.enabled’:False occurs, the nested dict which is not being set complains the feature.enabled is not True. This shouldnt happen because the nested dict is optional and is not being assigned, so it shouldnt validate. If the subfeature was required, than this behavior would make sense, but for optional fields, this is unexpected.
Issue Analytics
- State:
- Created 3 years ago
- Comments:8 (5 by maintainers)
Top GitHub Comments
i can reproduce the bug and i stripped the test case down so far that the
required
rule plays no role (except the default mentioned in the summary).i have looked around on SO and didn’t find a well-fitting question/answer. i also really can’t guess where your expectations diverge from the implementations.
let me try to point one thing out again: as you define a dfault for
subfeature
, the field will always be present in the document. thedepenedencies
rule defined on that field always expects theenabled
field to beTrue
. hence all documents where it holdsFalse
are evaluated as invalid.