Пример #1
0
    def check_type(self, value, attr, data):
        """Check if the type of the value is one of the possible choices.

        Possible choices are the model classes bound to the possible schemas.
        """
        if self.many and not is_collection(value):
            raise self._not_expected_type(value,
                                          Iterable,
                                          fields=[self],
                                          field_names=attr,
                                          data=data)

        _check_type = super().check_type

        errors = []
        values = value if self.many else [value]
        for idx, v in enumerate(values):
            try:
                _check_type(v, idx, values)
            except ValidationError as err:
                errors.append(err.messages)

        if errors:
            errors = errors if self.many else errors[0]
            raise ValidationError(errors)

        return value
    def check_type(self, value, attr, data):
        """Validate if the value is of the type of the schema's model.

        Assumes the nested schema is a ``BaseSchema``.
        """
        if self.many and not is_collection(value):
            raise self._not_expected_type(value,
                                          Iterable,
                                          fields=[self],
                                          field_names=attr,
                                          data=data)

        _check_type = super().check_type

        errors = []
        values = value if self.many else [value]
        for idx, v in enumerate(values):
            try:
                _check_type(v, idx, values)
            except ValidationError as err:
                errors.append(err.messages)

        if errors:
            errors = errors if self.many else errors[0]
            raise ValidationError(errors)

        return value
Пример #3
0
 def _serialize(self, value, key, obj):
     """Override ``_serialize`` for customizing the exception raised."""
     try:
         return super()._serialize(value, key, obj)
     except TypeError as ex:
         if 'serialization_schema_selector' in str(ex):
             raise ValidationError('Data from an invalid schema')
         raise
    def check_type(self, value, attr, data):
        """Validate if it's a list of valid item-field values.

        Check if each element in the list can be validated by the item-field
        passed during construction.
        """
        super().check_type(value, attr, data)

        errors = []
        for idx, v in enumerate(value):
            try:
                self.container.check_type(v, idx, value)
            except ValidationError as err:
                errors.append(err.messages)

        if errors:
            raise ValidationError(errors)

        return value