This commit is contained in:
Laurent Mazuel 2017-08-10 09:50:19 -07:00
Родитель 7ab47fee3c
Коммит ac28fb41ac
2 изменённых файлов: 17 добавлений и 7 удалений

Просмотреть файл

@ -158,14 +158,15 @@ class Model(object):
validation_result = [] validation_result = []
for attr_name, value in [(attr, getattr(self, attr)) for attr in self._attribute_map]: for attr_name, value in [(attr, getattr(self, attr)) for attr in self._attribute_map]:
attr_type = self._attribute_map[attr_name]['type'] attr_type = self._attribute_map[attr_name]['type']
try:
Serializer.validate(value, attr_name, **self._validation.get(attr_name, {}))
except ValidationError as validation_error:
validation_result.append(validation_error)
# If it's a list, try recursevly too
if attr_type[0] in ['[', '{']: if attr_type[0] in ['[', '{']:
validation_result += _validate_iterator_type(attr_type, value) validation_result += _validate_iterator_type(attr_type, value)
else:
try:
Serializer.validate(value, attr_name, **self._validation.get(attr_name, {}))
except ValidationError as validation_error:
validation_result.append(validation_error)
return validation_result return validation_result
def serialize(self): def serialize(self):

Просмотреть файл

@ -238,29 +238,38 @@ class TestRuntimeSerialized(unittest.TestCase):
_validation = { _validation = {
'name': {'min_length': 3}, 'name': {'min_length': 3},
'display_names': {'min_items': 2},
} }
_attribute_map = { _attribute_map = {
'name': {'key':'name', 'type':'str'}, 'name': {'key':'name', 'type':'str'},
'rec_list': {'key':'rec_list', 'type':'[[TestObj]]'}, 'rec_list': {'key':'rec_list', 'type':'[[TestObj]]'},
'rec_dict': {'key':'rec_dict', 'type':'{{TestObj}}'}, 'rec_dict': {'key':'rec_dict', 'type':'{{TestObj}}'},
'display_names': {'key': 'display_names', 'type': '[str]'},
} }
def __init__(self, name): def __init__(self, name):
self.name = name self.name = name
self.rec_list = None self.rec_list = None
self.rec_dict = None self.rec_dict = None
self.display_names = None
obj = TestObj("ab") obj = TestObj("ab")
obj.rec_list = [[TestObj("bc")]] obj.rec_list = [[TestObj("bc")]]
obj.rec_dict = {"key": {"key": TestObj("bc")}} obj.rec_dict = {"key": {"key": TestObj("bc")}}
obj.display_names = ["ab"]
broken_rules = obj.validate() broken_rules = obj.validate()
self.assertEquals(3, len(broken_rules)) self.assertEquals(4, len(broken_rules))
self.assertEquals( self.assertEquals(
"Parameter 'name' must have length greater than 3.", "Parameter 'name' must have length greater than 3.",
str(broken_rules[0]) str(broken_rules[0])
) )
self.assertEquals(
"Parameter 'display_names' must contain at least 2 items.",
str(broken_rules[3])
)
def test_obj_serialize_none(self): def test_obj_serialize_none(self):
"""Test that serialize None in object is still None. """Test that serialize None in object is still None.
""" """