diff --git a/cerberus/tests/tests.py b/cerberus/tests/tests.py index 14943c3b..cd3e4392 100644 --- a/cerberus/tests/tests.py +++ b/cerberus/tests/tests.py @@ -1580,6 +1580,36 @@ def test_coerce_non_digit_in_sequence(self): always_return_document=True), document) + def test_allow_unknown_with_of_rules(self): + # https://github.com/nicolaiarocci/cerberus/issues/251 + schema = { + 'test': { + 'oneof': [ + { + 'type': 'dict', + 'allow_unknown': True, + 'schema': {'known': {'type': 'string'}} + }, + { + 'type': 'dict', + 'schema': {'known': {'type': 'string'}} + }, + ] + } + } + # check regression and that allow unknown does not cause any different + # than expected behaviour for one-of. + document = {'test': {'known': 's'}} + self.validator(document, schema) + _errors = self.validator._errors + self.assertEqual(len(_errors), 1) + self.assertError('test', ('test', 'oneof'), + errors.ONEOF, schema['test']['oneof'], + v_errors=_errors) + # check that allow_unknown is actually applied + document = {'test': {'known': 's', 'unknown': 'asd'}} + self.assertNormalized(document, document, schema) + class TestDefinitionSchema(TestBase): def test_empty_schema(self): diff --git a/cerberus/validator.py b/cerberus/validator.py index 1ebb9c45..fb30a51d 100644 --- a/cerberus/validator.py +++ b/cerberus/validator.py @@ -931,7 +931,7 @@ def __validate_logical(self, operator, definitions, field, value): for rule in ('allow_unknown', 'type'): if rule not in schema[field] and rule in self.schema[field]: schema[field][rule] = self.schema[field][rule] - if 'allow_unknown' not in schema: + if 'allow_unknown' not in schema[field]: schema[field]['allow_unknown'] = self.allow_unknown validator = self._get_child_validator(