Skip to content

Commit

Permalink
Fixes schema rule when provided from rules registry
Browse files Browse the repository at this point in the history
Closes #599.
  • Loading branch information
funkyfuture committed Jul 28, 2023
1 parent 5421055 commit 585a200
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
19 changes: 18 additions & 1 deletion cerberus/tests/test_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

from pytest import mark

from cerberus import errors, Validator
from cerberus import errors, rules_set_registry, Validator
from cerberus.tests import (
assert_bad_type,
assert_document_error,
Expand Down Expand Up @@ -1987,3 +1987,20 @@ def test_allowed_when_passing_list_of_dicts():
(({'some': 'dict'},),),
),
)


def test_schema_validation_from_rules_set():
# https://github.com/pyeve/cerberus/issues/599
rules_set_registry.add(
'addr',
{
'type': 'dict',
'schema': {
'address': {'type': 'string'},
'city': {'type': 'string', 'required': True},
},
},
)
Validator(schema={'a_dict': 'addr'}).validate(
{'a_dict': {'address': 'my address', 'city': 'my town'}}
)
7 changes: 3 additions & 4 deletions cerberus/validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -1492,14 +1492,13 @@ def _validate_schema(self, schema, field, value):

def __validate_schema_mapping(self, field, schema, value):
schema = self._resolve_schema(schema)
allow_unknown = self.schema[field].get('allow_unknown', self.allow_unknown)
require_all = self.schema[field].get('require_all', self.require_all)
field_rules = self._resolve_rules_set(self.schema[field])
validator = self._get_child_validator(
document_crumb=field,
schema_crumb=(field, 'schema'),
schema=schema,
allow_unknown=allow_unknown,
require_all=require_all,
allow_unknown=field_rules.get('allow_unknown', self.allow_unknown),
require_all=field_rules.get('require_all', self.require_all),
)
try:
if not validator(value, update=self.update, normalize=False):
Expand Down

0 comments on commit 585a200

Please sign in to comment.