diff --git a/msrest/serialization.py b/msrest/serialization.py index 0ec1295..b5bce30 100644 --- a/msrest/serialization.py +++ b/msrest/serialization.py @@ -158,14 +158,15 @@ class Model(object): validation_result = [] for attr_name, value in [(attr, getattr(self, attr)) for attr in self._attribute_map]: 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 ['[', '{']: 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 def serialize(self): diff --git a/tests/test_serialization.py b/tests/test_serialization.py index c8795b3..9dc534f 100644 --- a/tests/test_serialization.py +++ b/tests/test_serialization.py @@ -238,29 +238,38 @@ class TestRuntimeSerialized(unittest.TestCase): _validation = { 'name': {'min_length': 3}, + 'display_names': {'min_items': 2}, } _attribute_map = { 'name': {'key':'name', 'type':'str'}, 'rec_list': {'key':'rec_list', 'type':'[[TestObj]]'}, 'rec_dict': {'key':'rec_dict', 'type':'{{TestObj}}'}, + 'display_names': {'key': 'display_names', 'type': '[str]'}, } def __init__(self, name): self.name = name self.rec_list = None self.rec_dict = None + self.display_names = None obj = TestObj("ab") obj.rec_list = [[TestObj("bc")]] obj.rec_dict = {"key": {"key": TestObj("bc")}} + obj.display_names = ["ab"] broken_rules = obj.validate() - self.assertEquals(3, len(broken_rules)) + self.assertEquals(4, len(broken_rules)) self.assertEquals( "Parameter 'name' must have length greater than 3.", 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): """Test that serialize None in object is still None. """