Exemple #1
0
    def __call__(self, value, context=None):
        if not is_sequence(value):
            self._fail('invalid', data=value)

        error_builder = ValidationErrorBuilder()

        for idx, item in enumerate(value):
            for validator in self.validators:
                try:
                    validator(item)
                except ValidationError as ve:
                    error_builder.add_errors({idx: ve.messages})

        error_builder.raise_errors()
Exemple #2
0
    def __call__(self, value, context=None):
        if not is_sequence(value):
            self._fail('invalid', data=value)

        error_builder = ValidationErrorBuilder()

        for idx, item in enumerate(value):
            for validator in self.validators:
                try:
                    validator(item)
                except ValidationError as ve:
                    error_builder.add_errors({idx: ve.messages})

        error_builder.raise_errors()
Exemple #3
0
    def dump(self, value, *args, **kwargs):
        if value is MISSING or value is None:
            self._fail('required')

        if not is_list(value):
            self._fail('invalid')

        errors_builder = ValidationErrorBuilder()
        items = []
        for idx, item in enumerate(value):
            try:
                items.append(self.item_type.dump(item, *args, **kwargs))
            except ValidationError as ve:
                errors_builder.add_errors({idx: ve.messages})
        errors_builder.raise_errors()

        return super(List, self).dump(items, *args, **kwargs)
Exemple #4
0
    def load(self, data, context=None):
        """Deserialize data from primitive types. Raises
        :exc:`~lollipop.errors.ValidationError` if data is invalid.

        :param data: Data to deserialize.
        :param context: Context data.
        :returns: Loaded data
        :raises: :exc:`~lollipop.errors.ValidationError`
        """
        errors_builder = ValidationErrorBuilder()
        for validator in self._validators:
            try:
                validator(data, context)
            except ValidationError as ve:
                errors_builder.add_errors(ve.messages)
        errors_builder.raise_errors()
        return data
Exemple #5
0
    def load(self, data, *args, **kwargs):
        if data is MISSING or data is None:
            self._fail('required')

        # TODO: Make more intelligent check for collections
        if not is_list(data):
            self._fail('invalid')

        errors_builder = ValidationErrorBuilder()
        items = []
        for idx, item in enumerate(data):
            try:
                items.append(self.item_type.load(item, *args, **kwargs))
            except ValidationError as ve:
                errors_builder.add_errors({idx: ve.messages})
        errors_builder.raise_errors()

        return super(List, self).load(items, *args, **kwargs)
Exemple #6
0
    def dump(self, value, *args, **kwargs):
        if value is MISSING or value is None:
            self._fail('required')

        if not is_list(value):
            self._fail('invalid')

        if len(value) != len(self.item_types):
            self._fail('invalid_length', expected_length=len(self.item_types))

        errors_builder = ValidationErrorBuilder()
        result = []
        for idx, (item_type, item) in enumerate(zip(self.item_types, value)):
            try:
                result.append(item_type.dump(item, *args, **kwargs))
            except ValidationError as ve:
                errors_builder.add_errors({idx: ve.messages})
        errors_builder.raise_errors()

        return super(Tuple, self).dump(result, *args, **kwargs)
Exemple #7
0
 def test_adding_merging_errors(self):
     builder = ValidationErrorBuilder()
     builder.add_errors({'foo': {'bar': 'error 1'}})
     builder.add_errors({'foo': {'baz': 'error 2'}})
     assert {'foo': {'bar': 'error 1', 'baz': 'error 2'}} == builder.errors
Exemple #8
0
 def test_adding_merging_errors(self):
     builder = ValidationErrorBuilder()
     builder.add_errors({'foo': {'bar': 'error 1'}})
     builder.add_errors({'foo': {'baz': 'error 2'}})
     assert {'foo': {'bar': 'error 1', 'baz': 'error 2'}} == builder.errors