Skip to content

utils

validators.utils.ValidationError

Bases: Exception

Exception class when validation failure occurs.

Source code in /opt/hostedtoolcache/Python/3.11.7/x64/lib/python3.11/site-packages/validators/utils.py
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
class ValidationError(Exception):
    """Exception class when validation failure occurs."""

    def __init__(self, function: Callable[..., Any], arg_dict: Dict[str, Any], message: str = ""):
        """Initialize Validation Failure."""
        if message:
            self.reason = message
        self.func = function
        self.__dict__.update(arg_dict)

    def __repr__(self):
        """Repr Validation Failure."""
        return (
            f"ValidationError(func={self.func.__name__}, "
            + f"args={({k: v for (k, v) in self.__dict__.items() if k != 'func'})})"
        )

    def __str__(self):
        """Str Validation Failure."""
        return repr(self)

    def __bool__(self):
        """Bool Validation Failure."""
        return False

__bool__()

Bool Validation Failure.

Source code in /opt/hostedtoolcache/Python/3.11.7/x64/lib/python3.11/site-packages/validators/utils.py
31
32
33
def __bool__(self):
    """Bool Validation Failure."""
    return False

__init__(function, arg_dict, message='')

Initialize Validation Failure.

Source code in /opt/hostedtoolcache/Python/3.11.7/x64/lib/python3.11/site-packages/validators/utils.py
13
14
15
16
17
18
def __init__(self, function: Callable[..., Any], arg_dict: Dict[str, Any], message: str = ""):
    """Initialize Validation Failure."""
    if message:
        self.reason = message
    self.func = function
    self.__dict__.update(arg_dict)

__repr__()

Repr Validation Failure.

Source code in /opt/hostedtoolcache/Python/3.11.7/x64/lib/python3.11/site-packages/validators/utils.py
20
21
22
23
24
25
def __repr__(self):
    """Repr Validation Failure."""
    return (
        f"ValidationError(func={self.func.__name__}, "
        + f"args={({k: v for (k, v) in self.__dict__.items() if k != 'func'})})"
    )

__str__()

Str Validation Failure.

Source code in /opt/hostedtoolcache/Python/3.11.7/x64/lib/python3.11/site-packages/validators/utils.py
27
28
29
def __str__(self):
    """Str Validation Failure."""
    return repr(self)

validators.utils.validator(func)

A decorator that makes given function validator.

Whenever the given func returns False this decorator returns ValidationError object.

Examples:

>>> @validator
... def even(value):
...     return not (value % 2)
>>> even(4)
# Output: True
>>> even(5)
# Output: ValidationError(func=even, args={'value': 5})

Parameters:

Name Type Description Default
func Callable[..., Any]

Function which is to be decorated.

required

Returns:

Type Description
Callable[..., ValidationError | Literal[True]]

A decorator which returns either ValidationError or Literal[True].

New in version 2013.10.21.

Source code in /opt/hostedtoolcache/Python/3.11.7/x64/lib/python3.11/site-packages/validators/utils.py
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
def validator(func: Callable[..., Any]):
    """A decorator that makes given function validator.

    Whenever the given `func` returns `False` this
    decorator returns `ValidationError` object.

    Examples:
        >>> @validator
        ... def even(value):
        ...     return not (value % 2)
        >>> even(4)
        # Output: True
        >>> even(5)
        # Output: ValidationError(func=even, args={'value': 5})

    Args:
        func:
            Function which is to be decorated.

    Returns:
        (Callable[..., ValidationError | Literal[True]]):
            A decorator which returns either `ValidationError`
            or `Literal[True]`.

    > *New in version 2013.10.21*.
    """

    @wraps(func)
    def wrapper(*args: Any, **kwargs: Any):
        try:
            return (
                True
                if func(*args, **kwargs)
                else ValidationError(func, _func_args_as_dict(func, *args, **kwargs))
            )
        except Exception as exp:
            return ValidationError(func, _func_args_as_dict(func, *args, **kwargs), str(exp))

    return wrapper