functional_validators.py 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828
  1. """This module contains related classes and functions for validation."""
  2. from __future__ import annotations as _annotations
  3. import dataclasses
  4. import sys
  5. from functools import partialmethod
  6. from types import FunctionType
  7. from typing import TYPE_CHECKING, Annotated, Any, Callable, Literal, TypeVar, Union, cast, overload
  8. from pydantic_core import PydanticUndefined, core_schema
  9. from pydantic_core import core_schema as _core_schema
  10. from typing_extensions import Self, TypeAlias
  11. from ._internal import _decorators, _generics, _internal_dataclass
  12. from .annotated_handlers import GetCoreSchemaHandler
  13. from .errors import PydanticUserError
  14. if sys.version_info < (3, 11):
  15. from typing_extensions import Protocol
  16. else:
  17. from typing import Protocol
  18. _inspect_validator = _decorators.inspect_validator
  19. @dataclasses.dataclass(frozen=True, **_internal_dataclass.slots_true)
  20. class AfterValidator:
  21. """!!! abstract "Usage Documentation"
  22. [field *after* validators](../concepts/validators.md#field-after-validator)
  23. A metadata class that indicates that a validation should be applied **after** the inner validation logic.
  24. Attributes:
  25. func: The validator function.
  26. Example:
  27. ```python
  28. from typing import Annotated
  29. from pydantic import AfterValidator, BaseModel, ValidationError
  30. MyInt = Annotated[int, AfterValidator(lambda v: v + 1)]
  31. class Model(BaseModel):
  32. a: MyInt
  33. print(Model(a=1).a)
  34. #> 2
  35. try:
  36. Model(a='a')
  37. except ValidationError as e:
  38. print(e.json(indent=2))
  39. '''
  40. [
  41. {
  42. "type": "int_parsing",
  43. "loc": [
  44. "a"
  45. ],
  46. "msg": "Input should be a valid integer, unable to parse string as an integer",
  47. "input": "a",
  48. "url": "https://errors.pydantic.dev/2/v/int_parsing"
  49. }
  50. ]
  51. '''
  52. ```
  53. """
  54. func: core_schema.NoInfoValidatorFunction | core_schema.WithInfoValidatorFunction
  55. def __get_pydantic_core_schema__(self, source_type: Any, handler: GetCoreSchemaHandler) -> core_schema.CoreSchema:
  56. schema = handler(source_type)
  57. info_arg = _inspect_validator(self.func, 'after')
  58. if info_arg:
  59. func = cast(core_schema.WithInfoValidatorFunction, self.func)
  60. return core_schema.with_info_after_validator_function(func, schema=schema, field_name=handler.field_name)
  61. else:
  62. func = cast(core_schema.NoInfoValidatorFunction, self.func)
  63. return core_schema.no_info_after_validator_function(func, schema=schema)
  64. @classmethod
  65. def _from_decorator(cls, decorator: _decorators.Decorator[_decorators.FieldValidatorDecoratorInfo]) -> Self:
  66. return cls(func=decorator.func)
  67. @dataclasses.dataclass(frozen=True, **_internal_dataclass.slots_true)
  68. class BeforeValidator:
  69. """!!! abstract "Usage Documentation"
  70. [field *before* validators](../concepts/validators.md#field-before-validator)
  71. A metadata class that indicates that a validation should be applied **before** the inner validation logic.
  72. Attributes:
  73. func: The validator function.
  74. json_schema_input_type: The input type of the function. This is only used to generate the appropriate
  75. JSON Schema (in validation mode).
  76. Example:
  77. ```python
  78. from typing import Annotated
  79. from pydantic import BaseModel, BeforeValidator
  80. MyInt = Annotated[int, BeforeValidator(lambda v: v + 1)]
  81. class Model(BaseModel):
  82. a: MyInt
  83. print(Model(a=1).a)
  84. #> 2
  85. try:
  86. Model(a='a')
  87. except TypeError as e:
  88. print(e)
  89. #> can only concatenate str (not "int") to str
  90. ```
  91. """
  92. func: core_schema.NoInfoValidatorFunction | core_schema.WithInfoValidatorFunction
  93. json_schema_input_type: Any = PydanticUndefined
  94. def __get_pydantic_core_schema__(self, source_type: Any, handler: GetCoreSchemaHandler) -> core_schema.CoreSchema:
  95. schema = handler(source_type)
  96. input_schema = (
  97. None
  98. if self.json_schema_input_type is PydanticUndefined
  99. else handler.generate_schema(self.json_schema_input_type)
  100. )
  101. info_arg = _inspect_validator(self.func, 'before')
  102. if info_arg:
  103. func = cast(core_schema.WithInfoValidatorFunction, self.func)
  104. return core_schema.with_info_before_validator_function(
  105. func,
  106. schema=schema,
  107. field_name=handler.field_name,
  108. json_schema_input_schema=input_schema,
  109. )
  110. else:
  111. func = cast(core_schema.NoInfoValidatorFunction, self.func)
  112. return core_schema.no_info_before_validator_function(
  113. func, schema=schema, json_schema_input_schema=input_schema
  114. )
  115. @classmethod
  116. def _from_decorator(cls, decorator: _decorators.Decorator[_decorators.FieldValidatorDecoratorInfo]) -> Self:
  117. return cls(
  118. func=decorator.func,
  119. json_schema_input_type=decorator.info.json_schema_input_type,
  120. )
  121. @dataclasses.dataclass(frozen=True, **_internal_dataclass.slots_true)
  122. class PlainValidator:
  123. """!!! abstract "Usage Documentation"
  124. [field *plain* validators](../concepts/validators.md#field-plain-validator)
  125. A metadata class that indicates that a validation should be applied **instead** of the inner validation logic.
  126. !!! note
  127. Before v2.9, `PlainValidator` wasn't always compatible with JSON Schema generation for `mode='validation'`.
  128. You can now use the `json_schema_input_type` argument to specify the input type of the function
  129. to be used in the JSON schema when `mode='validation'` (the default). See the example below for more details.
  130. Attributes:
  131. func: The validator function.
  132. json_schema_input_type: The input type of the function. This is only used to generate the appropriate
  133. JSON Schema (in validation mode). If not provided, will default to `Any`.
  134. Example:
  135. ```python
  136. from typing import Annotated, Union
  137. from pydantic import BaseModel, PlainValidator
  138. MyInt = Annotated[
  139. int,
  140. PlainValidator(
  141. lambda v: int(v) + 1, json_schema_input_type=Union[str, int] # (1)!
  142. ),
  143. ]
  144. class Model(BaseModel):
  145. a: MyInt
  146. print(Model(a='1').a)
  147. #> 2
  148. print(Model(a=1).a)
  149. #> 2
  150. ```
  151. 1. In this example, we've specified the `json_schema_input_type` as `Union[str, int]` which indicates to the JSON schema
  152. generator that in validation mode, the input type for the `a` field can be either a `str` or an `int`.
  153. """
  154. func: core_schema.NoInfoValidatorFunction | core_schema.WithInfoValidatorFunction
  155. json_schema_input_type: Any = Any
  156. def __get_pydantic_core_schema__(self, source_type: Any, handler: GetCoreSchemaHandler) -> core_schema.CoreSchema:
  157. # Note that for some valid uses of PlainValidator, it is not possible to generate a core schema for the
  158. # source_type, so calling `handler(source_type)` will error, which prevents us from generating a proper
  159. # serialization schema. To work around this for use cases that will not involve serialization, we simply
  160. # catch any PydanticSchemaGenerationError that may be raised while attempting to build the serialization schema
  161. # and abort any attempts to handle special serialization.
  162. from pydantic import PydanticSchemaGenerationError
  163. try:
  164. schema = handler(source_type)
  165. # TODO if `schema['serialization']` is one of `'include-exclude-dict/sequence',
  166. # schema validation will fail. That's why we use 'type ignore' comments below.
  167. serialization = schema.get(
  168. 'serialization',
  169. core_schema.wrap_serializer_function_ser_schema(
  170. function=lambda v, h: h(v),
  171. schema=schema,
  172. return_schema=handler.generate_schema(source_type),
  173. ),
  174. )
  175. except PydanticSchemaGenerationError:
  176. serialization = None
  177. input_schema = handler.generate_schema(self.json_schema_input_type)
  178. info_arg = _inspect_validator(self.func, 'plain')
  179. if info_arg:
  180. func = cast(core_schema.WithInfoValidatorFunction, self.func)
  181. return core_schema.with_info_plain_validator_function(
  182. func,
  183. field_name=handler.field_name,
  184. serialization=serialization, # pyright: ignore[reportArgumentType]
  185. json_schema_input_schema=input_schema,
  186. )
  187. else:
  188. func = cast(core_schema.NoInfoValidatorFunction, self.func)
  189. return core_schema.no_info_plain_validator_function(
  190. func,
  191. serialization=serialization, # pyright: ignore[reportArgumentType]
  192. json_schema_input_schema=input_schema,
  193. )
  194. @classmethod
  195. def _from_decorator(cls, decorator: _decorators.Decorator[_decorators.FieldValidatorDecoratorInfo]) -> Self:
  196. return cls(
  197. func=decorator.func,
  198. json_schema_input_type=decorator.info.json_schema_input_type,
  199. )
  200. @dataclasses.dataclass(frozen=True, **_internal_dataclass.slots_true)
  201. class WrapValidator:
  202. """!!! abstract "Usage Documentation"
  203. [field *wrap* validators](../concepts/validators.md#field-wrap-validator)
  204. A metadata class that indicates that a validation should be applied **around** the inner validation logic.
  205. Attributes:
  206. func: The validator function.
  207. json_schema_input_type: The input type of the function. This is only used to generate the appropriate
  208. JSON Schema (in validation mode).
  209. ```python
  210. from datetime import datetime
  211. from typing import Annotated
  212. from pydantic import BaseModel, ValidationError, WrapValidator
  213. def validate_timestamp(v, handler):
  214. if v == 'now':
  215. # we don't want to bother with further validation, just return the new value
  216. return datetime.now()
  217. try:
  218. return handler(v)
  219. except ValidationError:
  220. # validation failed, in this case we want to return a default value
  221. return datetime(2000, 1, 1)
  222. MyTimestamp = Annotated[datetime, WrapValidator(validate_timestamp)]
  223. class Model(BaseModel):
  224. a: MyTimestamp
  225. print(Model(a='now').a)
  226. #> 2032-01-02 03:04:05.000006
  227. print(Model(a='invalid').a)
  228. #> 2000-01-01 00:00:00
  229. ```
  230. """
  231. func: core_schema.NoInfoWrapValidatorFunction | core_schema.WithInfoWrapValidatorFunction
  232. json_schema_input_type: Any = PydanticUndefined
  233. def __get_pydantic_core_schema__(self, source_type: Any, handler: GetCoreSchemaHandler) -> core_schema.CoreSchema:
  234. schema = handler(source_type)
  235. input_schema = (
  236. None
  237. if self.json_schema_input_type is PydanticUndefined
  238. else handler.generate_schema(self.json_schema_input_type)
  239. )
  240. info_arg = _inspect_validator(self.func, 'wrap')
  241. if info_arg:
  242. func = cast(core_schema.WithInfoWrapValidatorFunction, self.func)
  243. return core_schema.with_info_wrap_validator_function(
  244. func,
  245. schema=schema,
  246. field_name=handler.field_name,
  247. json_schema_input_schema=input_schema,
  248. )
  249. else:
  250. func = cast(core_schema.NoInfoWrapValidatorFunction, self.func)
  251. return core_schema.no_info_wrap_validator_function(
  252. func,
  253. schema=schema,
  254. json_schema_input_schema=input_schema,
  255. )
  256. @classmethod
  257. def _from_decorator(cls, decorator: _decorators.Decorator[_decorators.FieldValidatorDecoratorInfo]) -> Self:
  258. return cls(
  259. func=decorator.func,
  260. json_schema_input_type=decorator.info.json_schema_input_type,
  261. )
  262. if TYPE_CHECKING:
  263. class _OnlyValueValidatorClsMethod(Protocol):
  264. def __call__(self, cls: Any, value: Any, /) -> Any: ...
  265. class _V2ValidatorClsMethod(Protocol):
  266. def __call__(self, cls: Any, value: Any, info: _core_schema.ValidationInfo, /) -> Any: ...
  267. class _OnlyValueWrapValidatorClsMethod(Protocol):
  268. def __call__(self, cls: Any, value: Any, handler: _core_schema.ValidatorFunctionWrapHandler, /) -> Any: ...
  269. class _V2WrapValidatorClsMethod(Protocol):
  270. def __call__(
  271. self,
  272. cls: Any,
  273. value: Any,
  274. handler: _core_schema.ValidatorFunctionWrapHandler,
  275. info: _core_schema.ValidationInfo,
  276. /,
  277. ) -> Any: ...
  278. _V2Validator = Union[
  279. _V2ValidatorClsMethod,
  280. _core_schema.WithInfoValidatorFunction,
  281. _OnlyValueValidatorClsMethod,
  282. _core_schema.NoInfoValidatorFunction,
  283. ]
  284. _V2WrapValidator = Union[
  285. _V2WrapValidatorClsMethod,
  286. _core_schema.WithInfoWrapValidatorFunction,
  287. _OnlyValueWrapValidatorClsMethod,
  288. _core_schema.NoInfoWrapValidatorFunction,
  289. ]
  290. _PartialClsOrStaticMethod: TypeAlias = Union[classmethod[Any, Any, Any], staticmethod[Any, Any], partialmethod[Any]]
  291. _V2BeforeAfterOrPlainValidatorType = TypeVar(
  292. '_V2BeforeAfterOrPlainValidatorType',
  293. bound=Union[_V2Validator, _PartialClsOrStaticMethod],
  294. )
  295. _V2WrapValidatorType = TypeVar('_V2WrapValidatorType', bound=Union[_V2WrapValidator, _PartialClsOrStaticMethod])
  296. FieldValidatorModes: TypeAlias = Literal['before', 'after', 'wrap', 'plain']
  297. @overload
  298. def field_validator(
  299. field: str,
  300. /,
  301. *fields: str,
  302. mode: Literal['wrap'],
  303. check_fields: bool | None = ...,
  304. json_schema_input_type: Any = ...,
  305. ) -> Callable[[_V2WrapValidatorType], _V2WrapValidatorType]: ...
  306. @overload
  307. def field_validator(
  308. field: str,
  309. /,
  310. *fields: str,
  311. mode: Literal['before', 'plain'],
  312. check_fields: bool | None = ...,
  313. json_schema_input_type: Any = ...,
  314. ) -> Callable[[_V2BeforeAfterOrPlainValidatorType], _V2BeforeAfterOrPlainValidatorType]: ...
  315. @overload
  316. def field_validator(
  317. field: str,
  318. /,
  319. *fields: str,
  320. mode: Literal['after'] = ...,
  321. check_fields: bool | None = ...,
  322. ) -> Callable[[_V2BeforeAfterOrPlainValidatorType], _V2BeforeAfterOrPlainValidatorType]: ...
  323. def field_validator(
  324. field: str,
  325. /,
  326. *fields: str,
  327. mode: FieldValidatorModes = 'after',
  328. check_fields: bool | None = None,
  329. json_schema_input_type: Any = PydanticUndefined,
  330. ) -> Callable[[Any], Any]:
  331. """!!! abstract "Usage Documentation"
  332. [field validators](../concepts/validators.md#field-validators)
  333. Decorate methods on the class indicating that they should be used to validate fields.
  334. Example usage:
  335. ```python
  336. from typing import Any
  337. from pydantic import (
  338. BaseModel,
  339. ValidationError,
  340. field_validator,
  341. )
  342. class Model(BaseModel):
  343. a: str
  344. @field_validator('a')
  345. @classmethod
  346. def ensure_foobar(cls, v: Any):
  347. if 'foobar' not in v:
  348. raise ValueError('"foobar" not found in a')
  349. return v
  350. print(repr(Model(a='this is foobar good')))
  351. #> Model(a='this is foobar good')
  352. try:
  353. Model(a='snap')
  354. except ValidationError as exc_info:
  355. print(exc_info)
  356. '''
  357. 1 validation error for Model
  358. a
  359. Value error, "foobar" not found in a [type=value_error, input_value='snap', input_type=str]
  360. '''
  361. ```
  362. For more in depth examples, see [Field Validators](../concepts/validators.md#field-validators).
  363. Args:
  364. field: The first field the `field_validator` should be called on; this is separate
  365. from `fields` to ensure an error is raised if you don't pass at least one.
  366. *fields: Additional field(s) the `field_validator` should be called on.
  367. mode: Specifies whether to validate the fields before or after validation.
  368. check_fields: Whether to check that the fields actually exist on the model.
  369. json_schema_input_type: The input type of the function. This is only used to generate
  370. the appropriate JSON Schema (in validation mode) and can only specified
  371. when `mode` is either `'before'`, `'plain'` or `'wrap'`.
  372. Returns:
  373. A decorator that can be used to decorate a function to be used as a field_validator.
  374. Raises:
  375. PydanticUserError:
  376. - If `@field_validator` is used bare (with no fields).
  377. - If the args passed to `@field_validator` as fields are not strings.
  378. - If `@field_validator` applied to instance methods.
  379. """
  380. if isinstance(field, FunctionType):
  381. raise PydanticUserError(
  382. '`@field_validator` should be used with fields and keyword arguments, not bare. '
  383. "E.g. usage should be `@validator('<field_name>', ...)`",
  384. code='validator-no-fields',
  385. )
  386. if mode not in ('before', 'plain', 'wrap') and json_schema_input_type is not PydanticUndefined:
  387. raise PydanticUserError(
  388. f"`json_schema_input_type` can't be used when mode is set to {mode!r}",
  389. code='validator-input-type',
  390. )
  391. if json_schema_input_type is PydanticUndefined and mode == 'plain':
  392. json_schema_input_type = Any
  393. fields = field, *fields
  394. if not all(isinstance(field, str) for field in fields):
  395. raise PydanticUserError(
  396. '`@field_validator` fields should be passed as separate string args. '
  397. "E.g. usage should be `@validator('<field_name_1>', '<field_name_2>', ...)`",
  398. code='validator-invalid-fields',
  399. )
  400. def dec(
  401. f: Callable[..., Any] | staticmethod[Any, Any] | classmethod[Any, Any, Any],
  402. ) -> _decorators.PydanticDescriptorProxy[Any]:
  403. if _decorators.is_instance_method_from_sig(f):
  404. raise PydanticUserError(
  405. '`@field_validator` cannot be applied to instance methods', code='validator-instance-method'
  406. )
  407. # auto apply the @classmethod decorator
  408. f = _decorators.ensure_classmethod_based_on_signature(f)
  409. dec_info = _decorators.FieldValidatorDecoratorInfo(
  410. fields=fields, mode=mode, check_fields=check_fields, json_schema_input_type=json_schema_input_type
  411. )
  412. return _decorators.PydanticDescriptorProxy(f, dec_info)
  413. return dec
  414. _ModelType = TypeVar('_ModelType')
  415. _ModelTypeCo = TypeVar('_ModelTypeCo', covariant=True)
  416. class ModelWrapValidatorHandler(_core_schema.ValidatorFunctionWrapHandler, Protocol[_ModelTypeCo]):
  417. """`@model_validator` decorated function handler argument type. This is used when `mode='wrap'`."""
  418. def __call__( # noqa: D102
  419. self,
  420. value: Any,
  421. outer_location: str | int | None = None,
  422. /,
  423. ) -> _ModelTypeCo: # pragma: no cover
  424. ...
  425. class ModelWrapValidatorWithoutInfo(Protocol[_ModelType]):
  426. """A `@model_validator` decorated function signature.
  427. This is used when `mode='wrap'` and the function does not have info argument.
  428. """
  429. def __call__( # noqa: D102
  430. self,
  431. cls: type[_ModelType],
  432. # this can be a dict, a model instance
  433. # or anything else that gets passed to validate_python
  434. # thus validators _must_ handle all cases
  435. value: Any,
  436. handler: ModelWrapValidatorHandler[_ModelType],
  437. /,
  438. ) -> _ModelType: ...
  439. class ModelWrapValidator(Protocol[_ModelType]):
  440. """A `@model_validator` decorated function signature. This is used when `mode='wrap'`."""
  441. def __call__( # noqa: D102
  442. self,
  443. cls: type[_ModelType],
  444. # this can be a dict, a model instance
  445. # or anything else that gets passed to validate_python
  446. # thus validators _must_ handle all cases
  447. value: Any,
  448. handler: ModelWrapValidatorHandler[_ModelType],
  449. info: _core_schema.ValidationInfo,
  450. /,
  451. ) -> _ModelType: ...
  452. class FreeModelBeforeValidatorWithoutInfo(Protocol):
  453. """A `@model_validator` decorated function signature.
  454. This is used when `mode='before'` and the function does not have info argument.
  455. """
  456. def __call__( # noqa: D102
  457. self,
  458. # this can be a dict, a model instance
  459. # or anything else that gets passed to validate_python
  460. # thus validators _must_ handle all cases
  461. value: Any,
  462. /,
  463. ) -> Any: ...
  464. class ModelBeforeValidatorWithoutInfo(Protocol):
  465. """A `@model_validator` decorated function signature.
  466. This is used when `mode='before'` and the function does not have info argument.
  467. """
  468. def __call__( # noqa: D102
  469. self,
  470. cls: Any,
  471. # this can be a dict, a model instance
  472. # or anything else that gets passed to validate_python
  473. # thus validators _must_ handle all cases
  474. value: Any,
  475. /,
  476. ) -> Any: ...
  477. class FreeModelBeforeValidator(Protocol):
  478. """A `@model_validator` decorated function signature. This is used when `mode='before'`."""
  479. def __call__( # noqa: D102
  480. self,
  481. # this can be a dict, a model instance
  482. # or anything else that gets passed to validate_python
  483. # thus validators _must_ handle all cases
  484. value: Any,
  485. info: _core_schema.ValidationInfo,
  486. /,
  487. ) -> Any: ...
  488. class ModelBeforeValidator(Protocol):
  489. """A `@model_validator` decorated function signature. This is used when `mode='before'`."""
  490. def __call__( # noqa: D102
  491. self,
  492. cls: Any,
  493. # this can be a dict, a model instance
  494. # or anything else that gets passed to validate_python
  495. # thus validators _must_ handle all cases
  496. value: Any,
  497. info: _core_schema.ValidationInfo,
  498. /,
  499. ) -> Any: ...
  500. ModelAfterValidatorWithoutInfo = Callable[[_ModelType], _ModelType]
  501. """A `@model_validator` decorated function signature. This is used when `mode='after'` and the function does not
  502. have info argument.
  503. """
  504. ModelAfterValidator = Callable[[_ModelType, _core_schema.ValidationInfo], _ModelType]
  505. """A `@model_validator` decorated function signature. This is used when `mode='after'`."""
  506. _AnyModelWrapValidator = Union[ModelWrapValidator[_ModelType], ModelWrapValidatorWithoutInfo[_ModelType]]
  507. _AnyModelBeforeValidator = Union[
  508. FreeModelBeforeValidator, ModelBeforeValidator, FreeModelBeforeValidatorWithoutInfo, ModelBeforeValidatorWithoutInfo
  509. ]
  510. _AnyModelAfterValidator = Union[ModelAfterValidator[_ModelType], ModelAfterValidatorWithoutInfo[_ModelType]]
  511. @overload
  512. def model_validator(
  513. *,
  514. mode: Literal['wrap'],
  515. ) -> Callable[
  516. [_AnyModelWrapValidator[_ModelType]], _decorators.PydanticDescriptorProxy[_decorators.ModelValidatorDecoratorInfo]
  517. ]: ...
  518. @overload
  519. def model_validator(
  520. *,
  521. mode: Literal['before'],
  522. ) -> Callable[
  523. [_AnyModelBeforeValidator], _decorators.PydanticDescriptorProxy[_decorators.ModelValidatorDecoratorInfo]
  524. ]: ...
  525. @overload
  526. def model_validator(
  527. *,
  528. mode: Literal['after'],
  529. ) -> Callable[
  530. [_AnyModelAfterValidator[_ModelType]], _decorators.PydanticDescriptorProxy[_decorators.ModelValidatorDecoratorInfo]
  531. ]: ...
  532. def model_validator(
  533. *,
  534. mode: Literal['wrap', 'before', 'after'],
  535. ) -> Any:
  536. """!!! abstract "Usage Documentation"
  537. [Model Validators](../concepts/validators.md#model-validators)
  538. Decorate model methods for validation purposes.
  539. Example usage:
  540. ```python
  541. from typing_extensions import Self
  542. from pydantic import BaseModel, ValidationError, model_validator
  543. class Square(BaseModel):
  544. width: float
  545. height: float
  546. @model_validator(mode='after')
  547. def verify_square(self) -> Self:
  548. if self.width != self.height:
  549. raise ValueError('width and height do not match')
  550. return self
  551. s = Square(width=1, height=1)
  552. print(repr(s))
  553. #> Square(width=1.0, height=1.0)
  554. try:
  555. Square(width=1, height=2)
  556. except ValidationError as e:
  557. print(e)
  558. '''
  559. 1 validation error for Square
  560. Value error, width and height do not match [type=value_error, input_value={'width': 1, 'height': 2}, input_type=dict]
  561. '''
  562. ```
  563. For more in depth examples, see [Model Validators](../concepts/validators.md#model-validators).
  564. Args:
  565. mode: A required string literal that specifies the validation mode.
  566. It can be one of the following: 'wrap', 'before', or 'after'.
  567. Returns:
  568. A decorator that can be used to decorate a function to be used as a model validator.
  569. """
  570. def dec(f: Any) -> _decorators.PydanticDescriptorProxy[Any]:
  571. # auto apply the @classmethod decorator
  572. f = _decorators.ensure_classmethod_based_on_signature(f)
  573. dec_info = _decorators.ModelValidatorDecoratorInfo(mode=mode)
  574. return _decorators.PydanticDescriptorProxy(f, dec_info)
  575. return dec
  576. AnyType = TypeVar('AnyType')
  577. if TYPE_CHECKING:
  578. # If we add configurable attributes to IsInstance, we'd probably need to stop hiding it from type checkers like this
  579. InstanceOf = Annotated[AnyType, ...] # `IsInstance[Sequence]` will be recognized by type checkers as `Sequence`
  580. else:
  581. @dataclasses.dataclass(**_internal_dataclass.slots_true)
  582. class InstanceOf:
  583. '''Generic type for annotating a type that is an instance of a given class.
  584. Example:
  585. ```python
  586. from pydantic import BaseModel, InstanceOf
  587. class Foo:
  588. ...
  589. class Bar(BaseModel):
  590. foo: InstanceOf[Foo]
  591. Bar(foo=Foo())
  592. try:
  593. Bar(foo=42)
  594. except ValidationError as e:
  595. print(e)
  596. """
  597. [
  598. │ {
  599. │ │ 'type': 'is_instance_of',
  600. │ │ 'loc': ('foo',),
  601. │ │ 'msg': 'Input should be an instance of Foo',
  602. │ │ 'input': 42,
  603. │ │ 'ctx': {'class': 'Foo'},
  604. │ │ 'url': 'https://errors.pydantic.dev/0.38.0/v/is_instance_of'
  605. │ }
  606. ]
  607. """
  608. ```
  609. '''
  610. @classmethod
  611. def __class_getitem__(cls, item: AnyType) -> AnyType:
  612. return Annotated[item, cls()]
  613. @classmethod
  614. def __get_pydantic_core_schema__(cls, source: Any, handler: GetCoreSchemaHandler) -> core_schema.CoreSchema:
  615. from pydantic import PydanticSchemaGenerationError
  616. # use the generic _origin_ as the second argument to isinstance when appropriate
  617. instance_of_schema = core_schema.is_instance_schema(_generics.get_origin(source) or source)
  618. try:
  619. # Try to generate the "standard" schema, which will be used when loading from JSON
  620. original_schema = handler(source)
  621. except PydanticSchemaGenerationError:
  622. # If that fails, just produce a schema that can validate from python
  623. return instance_of_schema
  624. else:
  625. # Use the "original" approach to serialization
  626. instance_of_schema['serialization'] = core_schema.wrap_serializer_function_ser_schema(
  627. function=lambda v, h: h(v), schema=original_schema
  628. )
  629. return core_schema.json_or_python_schema(python_schema=instance_of_schema, json_schema=original_schema)
  630. __hash__ = object.__hash__
  631. if TYPE_CHECKING:
  632. SkipValidation = Annotated[AnyType, ...] # SkipValidation[list[str]] will be treated by type checkers as list[str]
  633. else:
  634. @dataclasses.dataclass(**_internal_dataclass.slots_true)
  635. class SkipValidation:
  636. """If this is applied as an annotation (e.g., via `x: Annotated[int, SkipValidation]`), validation will be
  637. skipped. You can also use `SkipValidation[int]` as a shorthand for `Annotated[int, SkipValidation]`.
  638. This can be useful if you want to use a type annotation for documentation/IDE/type-checking purposes,
  639. and know that it is safe to skip validation for one or more of the fields.
  640. Because this converts the validation schema to `any_schema`, subsequent annotation-applied transformations
  641. may not have the expected effects. Therefore, when used, this annotation should generally be the final
  642. annotation applied to a type.
  643. """
  644. def __class_getitem__(cls, item: Any) -> Any:
  645. return Annotated[item, SkipValidation()]
  646. @classmethod
  647. def __get_pydantic_core_schema__(cls, source: Any, handler: GetCoreSchemaHandler) -> core_schema.CoreSchema:
  648. original_schema = handler(source)
  649. metadata = {'pydantic_js_annotation_functions': [lambda _c, h: h(original_schema)]}
  650. return core_schema.any_schema(
  651. metadata=metadata,
  652. serialization=core_schema.wrap_serializer_function_ser_schema(
  653. function=lambda v, h: h(v), schema=original_schema
  654. ),
  655. )
  656. __hash__ = object.__hash__