|
1
|
|
|
""" |
|
2
|
|
|
PRIVATE MODULE: do not import (from) it directly. |
|
3
|
|
|
|
|
4
|
|
|
This module contains functionality for validating objects. |
|
5
|
|
|
""" |
|
6
|
|
|
from typing import Union, Sequence, Callable |
|
7
|
|
|
|
|
8
|
|
|
from jsons._cache import cached |
|
9
|
|
|
from jsons._common_impl import StateHolder, get_class_name |
|
10
|
|
|
from jsons._lizers_impl import _get_lizer |
|
11
|
|
|
from jsons.exceptions import ValidationError |
|
12
|
|
|
|
|
13
|
|
|
|
|
14
|
|
|
def set_validator( |
|
15
|
|
|
func: Callable[[object], bool], |
|
16
|
|
|
cls: Union[type, Sequence[type]], |
|
17
|
|
|
*, |
|
18
|
|
|
fork_inst: type = StateHolder) -> None: |
|
19
|
|
|
""" |
|
20
|
|
|
Set a validator function for the given ``cls``. The function should accept |
|
21
|
|
|
an instance of the type it should validate and must return ``False`` or |
|
22
|
|
|
raise any exception in case of a validation failure. |
|
23
|
|
|
:param func: the function that takes an instance of type ``cls`` and |
|
24
|
|
|
returns a bool (``True`` if the validation was successful). |
|
25
|
|
|
:param cls: the type or types that ``func`` is able to validate. |
|
26
|
|
|
:param fork_inst: if given, it uses this fork of ``JsonSerializable``. |
|
27
|
|
|
:return: None. |
|
28
|
|
|
""" |
|
29
|
|
|
if isinstance(cls, Sequence): |
|
30
|
|
|
for cls_ in cls: |
|
31
|
|
|
set_validator(func, cls=cls_, fork_inst=fork_inst) |
|
32
|
|
|
else: |
|
33
|
|
|
cls_name = get_class_name(cls, fully_qualified=True) |
|
34
|
|
|
fork_inst._validators[cls_name.lower()] = func |
|
35
|
|
|
fork_inst._classes_validators.append(cls) |
|
36
|
|
|
|
|
37
|
|
|
|
|
38
|
|
|
@cached |
|
39
|
|
|
def get_validator( |
|
40
|
|
|
cls: type, |
|
41
|
|
|
fork_inst: type = StateHolder) -> callable: |
|
42
|
|
|
""" |
|
43
|
|
|
Return the validator function that would be used for the given ``cls``. |
|
44
|
|
|
:param cls: the type for which a deserializer is to be returned. |
|
45
|
|
|
:param fork_inst: if given, it uses this fork of ``JsonSerializable``. |
|
46
|
|
|
:return: a validator function. |
|
47
|
|
|
""" |
|
48
|
|
|
return _get_lizer(cls, fork_inst._validators, |
|
49
|
|
|
fork_inst._classes_validators, fork_inst) |
|
50
|
|
|
|
|
51
|
|
|
|
|
52
|
|
|
def validate( |
|
53
|
|
|
obj: object, |
|
54
|
|
|
cls: type, |
|
55
|
|
|
fork_inst: type = StateHolder) -> None: |
|
56
|
|
|
""" |
|
57
|
|
|
Validate the given ``obj`` with the validator that was registered for |
|
58
|
|
|
``cls``. Raises a ``ValidationError`` if the validation failed. |
|
59
|
|
|
:param obj: the object that is to be validated. |
|
60
|
|
|
:param cls: the type of which the validator function was registered. |
|
61
|
|
|
:param fork_inst: if given, it uses this fork of ``JsonSerializable``. |
|
62
|
|
|
:return: None. |
|
63
|
|
|
""" |
|
64
|
|
|
validator = get_validator(cls, fork_inst) |
|
65
|
|
|
result = True |
|
66
|
|
|
msg = 'Validation failed.' |
|
67
|
|
|
if validator: |
|
68
|
|
|
try: |
|
69
|
|
|
result = validator(obj) |
|
70
|
|
|
except Exception as err: |
|
71
|
|
|
if err.args: |
|
72
|
|
|
msg = err.args[0] |
|
73
|
|
|
result = False |
|
74
|
|
|
if not result: |
|
75
|
|
|
raise ValidationError(msg) |
|
76
|
|
|
|