Beispiel #1
0
    def contains_(value):
        """
        Validate that the given list/range/tuple contains the given value.

        Raises:
            ValidationError: when the value is not one of the allowed values.
        """
        if value not in allowed:
            raise ValidationError('{!r} is not a valid choice'.format(value))
Beispiel #2
0
    def min_(value):
        """
        Validate that the given value is greater than and/or equal to a minimum.

        Args:
            value: the value to validate.

        ValidationError: when the value is greater than and/or equal to the
                maximum.
        """
        if inclusive:
            if value < endpoint:
                raise ValidationError(
                    'expected at least {!r} but got {!r}'.format(
                        endpoint, value))
        else:
            if value <= endpoint:
                raise ValidationError(
                    'expected more than {!r} but got {!r}'.format(
                        endpoint, value))
Beispiel #3
0
    def max_(value):
        """
        Validate that the given value is less than and/or equal to the maximum.

        Args:
            value: the value to validate.

        Raises:
            ValidationError: when the value is not less than and/or equal to the
                maximum.
        """
        if inclusive:
            if value > endpoint:
                raise ValidationError(
                    'expected at most {!r} but got {!r}'.format(
                        endpoint, value))
        else:
            if value >= endpoint:
                raise ValidationError(
                    'expected less than {!r} but got {!r}'.format(
                        endpoint, value))
Beispiel #4
0
def url(value):
    """
    Validate whether or not the given string is a valid URL.

    Args:
        value (str): the value to validate.

    Raises:
        ValidationError: when the value is not a valid URL.
    """
    if not validators.url(value):
        raise ValidationError('{!r} is not a valid URL'.format(value))
Beispiel #5
0
def mac_address(value):
    """
    Validate whether or not the given string is a valid MAC address.

    Args:
        value (str): the value to validate.

    Raises:
        ValidationError: when the value is not a valid MAC address.
    """
    if not validators.mac_address(value):
        raise ValidationError('{!r} is not a valid MAC address'.format(value))
Beispiel #6
0
    def instance_(value):
        """
        Validate that the given value is an instance of a type.

        Args:
            value: the value to validate.

        Raises:
            ValidationError: when the value is not an instance of the type.
        """
        if not isinstance(value, type):
            raise ValidationError('expected {!r} but got {!r}'.format(
                type.__name__, value.__class__.__name__))
Beispiel #7
0
    def validate_all(self):
        """
        Validate all Fields on this Model, and the Model itself.

        This is called by the Model constructor, so this is only needed if you
        modify attributes directly and want to revalidate the Model.
        """
        for name, field in self._fields.items():
            value = getattr(self, name)

            if value is None:
                if field.required:
                    raise ValidationError('{!r} is required'.format(name), field=field, model=self)
            else:
                self._validate_field(field, value)

        map_errors(ValidationError, model=self)(self.validate)()