I'm not sure if I understand the 'allow_unknown' rule correctly:
I can do this
import cerberus
schema = {
'test': {
'schema': {'a': {'type': 'string'}}, 'type': 'dict', 'allow_unknown':True
}
}
validator = cerberus.Validator(schema)
validator.validate({'test': {'unknown_key': 'unknown_value'}})
validator.validate({'test': {}})
and both validations return True.
But if I nest the dictionary inside a list and try to do the same:
schema = {
'test': {
'type': 'list',
'schema': {
'type': 'dict',
'allow_unknown':True,
'schema': {'a': {'type': 'string'}}
}
}
}
validator = cerberus.Validator(schema)
validator.validate({'test': [{'unknown_key': 'unknown_value'}]})
validator.validate({'test': {}})
unexpected things (for me at least) happen.
The first validation results in True as expected. For the second however, I would have expected it to return False and indicate an error (because of the dict was used instead of the list) within the errors attribute. Instead, cerberus throws a TypeError while calling getitem on the schema using the 'allow_unknownvalueTrue` which of course fails.
Am I using the rule incorrectly? I am using cerberus 1.0rc0 pulled directly from github.
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-44-bead38c40317> in <module>()
----> 1 validator.validate({'test': {}})
/pythonenv/lib/python3.5/site-packages/cerberus/validator.py in validate(self, document, schema, update, normalize)
730 if normalize:
731 self.__normalize_mapping(self.document, self.schema)
--> 732
733 for field in self.document:
734 if self.ignore_none_values and self.document[field] is None:
/pythonenv/lib/python3.5/site-packages/cerberus/validator.py in __normalize_mapping(self, mapping, schema)
497 self.__normalize_default_fields(mapping, schema)
498 self._normalize_coerce(mapping, schema)
--> 499 self.__normalize_containers(mapping, schema)
500 return mapping
501
/pythonenv/lib/python3.5/site-packages/cerberus/validator.py in __normalize_containers(self, mapping, schema)
552 if set(schema[field]) & set(('allow_unknown', 'purge_unknown',
553 'schema')):
--> 554 self.__normalize_mapping_per_schema(field, mapping, schema)
555 elif isinstance(mapping[field], _str_type):
556 continue
/pythonenv/lib/python3.5/site-packages/cerberus/validator.py in __normalize_mapping_per_schema(self, field, mapping, schema)
600 purge_unknown=schema[field].get('purge_unknown', self.purge_unknown)) # noqa
601 mapping[field] = validator.normalized(mapping[field],
--> 602 always_return_document=True)
603 if validator._errors:
604 self._error(validator._errors)
/pythonenv/lib/python3.5/site-packages/cerberus/validator.py in normalized(self, document, schema, always_return_document)
484 """
485 self.__init_processing(document, schema)
--> 486 self.__normalize_mapping(self.document, self.schema)
487 self.error_handler.end(self)
488 if self._errors and not always_return_document:
/pythonenv/lib/python3.5/site-packages/cerberus/validator.py in __normalize_mapping(self, mapping, schema)
495 if self.purge_unknown:
496 self._normalize_purge_unknown(mapping, schema)
--> 497 self.__normalize_default_fields(mapping, schema)
498 self._normalize_coerce(mapping, schema)
499 self.__normalize_containers(mapping, schema)
/pythonenv/lib/python3.5/site-packages/cerberus/validator.py in __normalize_default_fields(self, mapping, schema)
665
666 for field in [x for x in fields if 'default' in schema[x]]:
--> 667 self._normalize_default(mapping, schema, field)
668
669 known_fields_states = set()
/pythonenv/lib/python3.5/site-packages/cerberus/validator.py in <listcomp>(.0)
665
666 for field in [x for x in fields if 'default' in schema[x]]:
--> 667 self._normalize_default(mapping, schema, field)
668
669 known_fields_states = set()
TypeError: argument of type 'bool' is not iterable
I'm not sure if I understand the 'allow_unknown' rule correctly:
I can do this
and both validations return
True.But if I nest the dictionary inside a list and try to do the same:
unexpected things (for me at least) happen.
The first validation results in
Trueas expected. For the second however, I would have expected it to returnFalseand indicate an error (because of the dict was used instead of the list) within theerrorsattribute. Instead, cerberus throws aTypeErrorwhile calling getitem on the schema using the 'allow_unknownvalueTrue` which of course fails.Am I using the rule incorrectly? I am using cerberus 1.0rc0 pulled directly from github.