Move additional props check to separate method

This commit is contained in:
Artur Maciag 2019-03-26 13:27:44 +00:00
parent 14196b6ce1
commit 4d99cbe7e1
2 changed files with 21 additions and 20 deletions

View file

@ -161,6 +161,13 @@ class Schema(object):
return defaultdict(lambda: lambda x: x, mapping)
def are_additional_properties_allowed(self, one_of_schema=None):
return (
(self.additional_properties is not False) and
(one_of_schema is None or
one_of_schema.additional_properties is not False)
)
def cast(self, value, custom_formatters=None, strict=True):
"""Cast value to schema type"""
if value is None:
@ -311,12 +318,9 @@ class Schema(object):
value_props_names = value.keys()
extra_props = set(value_props_names) - set(all_props_names)
no_more_properties_allowed = (
(self.additional_properties is False) or
(one_of_schema is not None and
one_of_schema.additional_properties is False)
)
if extra_props and no_more_properties_allowed:
extra_props_allowed = self.are_additional_properties_allowed(
one_of_schema)
if extra_props and not extra_props_allowed:
raise UndefinedSchemaProperty(extra_props)
properties = {}
@ -548,12 +552,9 @@ class Schema(object):
value_props_names = value.keys()
extra_props = set(value_props_names) - set(all_props_names)
no_more_properties_allowed = (
(self.additional_properties is False) or
(one_of_schema is not None and
one_of_schema.additional_properties is False)
)
if extra_props and no_more_properties_allowed:
extra_props_allowed = self.are_additional_properties_allowed(
one_of_schema)
if extra_props and not extra_props_allowed:
raise UndefinedSchemaProperty(extra_props)
if self.additional_properties is not True:

View file

@ -713,11 +713,11 @@ class TestSchemaValidate(object):
@pytest.mark.parametrize('value', [
Model({
u'foo': u'FOO',
'foo': u("FOO"),
}),
Model({
u'foo': u'FOO',
u'bar': u'BAR',
'foo': u("FOO"),
'bar': u("BAR"),
}),
])
def test_unambiguous_one_of(self, value):
@ -725,19 +725,19 @@ class TestSchemaValidate(object):
Schema(
'object',
properties={
u'foo': Schema('string'),
'foo': Schema('string'),
},
additional_properties=False,
required=[u'foo'],
required=['foo'],
),
Schema(
'object',
properties={
u'foo': Schema('string'),
u'bar': Schema('string'),
'foo': Schema('string'),
'bar': Schema('string'),
},
additional_properties=False,
required=[u'foo', u'bar'],
required=['foo', 'bar'],
),
]
schema = Schema('object', one_of=one_of)