New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dependencies #138

Closed
kseniyam opened this Issue Sep 4, 2015 · 5 comments

Comments

Projects
None yet
3 participants
@kseniyam
Copy link

kseniyam commented Sep 4, 2015

Has something gone wrong with dependencies checking, or I simply do not get the way it is supposed to work?
schema = {'text':{'dependencies':{'deleted':False}}, 'deleted':{'type':'boolean'}}
test = {'deleted':True}
v.validate(test, schema)
True
test = {'deleted':False}
v.validate(test, schema)
True

I use 0.9.1

@nicolaiarocci

This comment has been minimized.

Copy link
Member

nicolaiarocci commented Sep 5, 2015

Your schema implies that deleted is a text dependency which means that text is only valid if deleted is there. Try with:

v.validate({'text': 'hello'}, schema)
False
v.validate({'text': 'hello', 'deleted': False}, schema)
True

Since your documents only have deleted, which has no dependancy, validation returns True as long as the type is bool.

@rredkovich

This comment has been minimized.

Copy link
Contributor

rredkovich commented Sep 5, 2015

It seems that with dependency on boolean field cerberus doesn't check value of that field.

Dependency on text field:

schema = {'deleted': {'type': 'string'}, 'text': {'dependencies': {'deleted':['foo', 'bar']}}}
doc = {'text':'abc', 'deleted':'q'}
v.validate(doc, schema)
v.errors
{'text': "field '('deleted', ['foo', 'bar'])' is required with values: ('deleted', ['foo', 'bar'])"}

boolean field:

schema = {'deleted': {'type': 'boolean'}, 'text': {'dependencies': {'deleted':True}}}
doc = {'text':'abc'}
v.validate(doc, schema)
v.errors
{'text': "field '('deleted', True)' is required with values: ('deleted', True)"}

schema = {'deleted': {'type': 'boolean'}, 'text': {'dependencies': {'deleted':True}}}
doc = {'text':'abc', 'deleted': 1}
v.validate(doc, schema)
v.errors
{'deleted': 'must be of boolean type'}

schema = {'deleted': {'type': 'boolean'}, 'text': {'dependencies': {'deleted':[True]}}}
doc = {'text':'abc', 'deleted': False}
v.validate(doc, schema)
True

schema = {'deleted': {'type': 'boolean'}, 'text': {'dependencies': {'deleted':True}}}
doc = {'text':'abc', 'deleted': False}
v.validate(doc, schema)
True
@nicolaiarocci

This comment has been minimized.

Copy link
Member

nicolaiarocci commented Sep 5, 2015

Yeah I noticed that too.

@nicolaiarocci nicolaiarocci added the bug label Sep 5, 2015

@nicolaiarocci nicolaiarocci added this to the 0.10 milestone Sep 5, 2015

@rredkovich

This comment has been minimized.

Copy link
Contributor

rredkovich commented Sep 5, 2015

Actual github code (branch master) has different behaviour, works correct with boolean dependency in list but raises an exception with single boolean

def main():
    v = cerberus.Validator()

    schema = {'deleted': {'type': 'string'},
              'text': {'dependencies': {'deleted': 'foo'}}}
    doc = {'text': 'abc', 'deleted': 'bar'}
    print(v.validate(doc, schema))
    print(v.errors, '\n')

    schema = {'deleted': {'type': 'boolean'},
              'text': {'dependencies': {'deleted': [True]}}}
    doc = {'text':'abc', 'deleted': False}
    print(v.validate(doc, schema))
    print(v.errors, '\n')

    schema = {'deleted': {'type': 'boolean'},
              'text': {'dependencies': {'deleted': True}}}
    doc = {'text':'abc', 'deleted': False}
    print(v.validate(doc, schema))
    print(v.errors, '\n')
~/Developer/cerberus  master ✗ 
▶ python3 test_bool_depend.py
False
{'text': "field 'deleted' is required with one of these values: ['foo']"} 

False
{'text': "field 'deleted' is required with one of these values: [True]"} 

Traceback (most recent call last):
  File "test_bool_depend.py", line 27, in <module>
    main()
  File "test_bool_depend.py", line 22, in main
    print(v.validate(doc, schema))
  File "/Users/devilandall/Developer/cerberus/cerberus/cerberus.py", line 427, in validate
    self.__validate_definition(definition, field)
  File "/Users/devilandall/Developer/cerberus/cerberus/cerberus.py", line 509, in __validate_definition
    self._validate_dependencies(definition, field, value) or
  File "/Users/devilandall/Developer/cerberus/cerberus/cerberus.py", line 543, in _validate_dependencies
    self.__validate_dependencies_mapping(dependencies, field)
  File "/Users/devilandall/Developer/cerberus/cerberus/cerberus.py", line 559, in __validate_dependencies_mapping
    if context in dep_values:
TypeError: argument of type 'bool' is not iterable

rredkovich added a commit to rredkovich/cerberus that referenced this issue Sep 6, 2015

@nicolaiarocci

This comment has been minimized.

Copy link
Member

nicolaiarocci commented Sep 7, 2015

Fixed with #139

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment