MCPcopy
hub / github.com/pydantic/pydantic / validator

Function validator

pydantic/v1/class_validators.py:52–104  ·  view source on GitHub ↗

Decorate methods on the class indicating that they should be used to validate fields :param fields: which field(s) the method should be called on :param pre: whether or not this validator should be called before the standard validators (else after) :param each_item: for complex obje

(
    *fields: str,
    pre: bool = False,
    each_item: bool = False,
    always: bool = False,
    check_fields: bool = True,
    whole: Optional[bool] = None,
    allow_reuse: bool = False,
)

Source from the content-addressed store, hash-verified

50
51
52def validator(
53 *fields: str,
54 pre: bool = False,
55 each_item: bool = False,
56 always: bool = False,
57 check_fields: bool = True,
58 whole: Optional[bool] = None,
59 allow_reuse: bool = False,
60) -> Callable[[AnyCallable], 'AnyClassMethod']:
61 """
62 Decorate methods on the class indicating that they should be used to validate fields
63 :param fields: which field(s) the method should be called on
64 :param pre: whether or not this validator should be called before the standard validators (else after)
65 :param each_item: for complex objects (sets, lists etc.) whether to validate individual elements rather than the
66 whole object
67 :param always: whether this method and other validators should be called even if the value is missing
68 :param check_fields: whether to check that the fields actually exist on the model
69 :param allow_reuse: whether to track and raise an error if another validator refers to the decorated function
70 """
71 if not fields:
72 raise ConfigError('validator with no fields specified')
73 elif isinstance(fields[0], FunctionType):
74 raise ConfigError(
75 "validators should be used with fields and keyword arguments, not bare. " # noqa: Q000
76 "E.g. usage should be `@validator('<field_name>', ...)`"
77 )
78 elif not all(isinstance(field, str) for field in fields):
79 raise ConfigError(
80 "validator fields should be passed as separate string args. " # noqa: Q000
81 "E.g. usage should be `@validator('<field_name_1>', '<field_name_2>', ...)`"
82 )
83
84 if whole is not None:
85 warnings.warn(
86 'The "whole" keyword argument is deprecated, use "each_item" (inverse meaning, default False) instead',
87 DeprecationWarning,
88 )
89 assert each_item is False, '"each_item" and "whole" conflict, remove "whole"'
90 each_item = not whole
91
92 def dec(f: AnyCallable) -> 'AnyClassMethod':
93 f_cls = _prepare_validator(f, allow_reuse)
94 setattr(
95 f_cls,
96 VALIDATOR_CONFIG_KEY,
97 (
98 fields,
99 Validator(func=f_cls.__func__, pre=pre, each_item=each_item, always=always, check_fields=check_fields),
100 ),
101 )
102 return f_cls
103
104 return dec
105
106
107@overload

Callers 12

ModelClass · 0.90
my_wrap_validatorMethod · 0.90
_apply_validatorsMethod · 0.70
_generic_validator_clsFunction · 0.70
_generic_validator_basicFunction · 0.70
__setattr__Method · 0.70
validate_modelFunction · 0.70
sequence_validatorFunction · 0.50
_wrapper1Function · 0.50
_wrapper2Function · 0.50

Calls 1

ConfigErrorClass · 0.90

Tested by 3

my_wrap_validatorMethod · 0.72