functional_validators.py 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883
  1. """This module contains related classes and functions for validation."""
  2. from __future__ import annotations as _annotations
  3. import dataclasses
  4. import sys
  5. import warnings
  6. from functools import partialmethod
  7. from types import FunctionType
  8. from typing import TYPE_CHECKING, Annotated, Any, Callable, Literal, TypeVar, Union, cast, overload
  9. from pydantic_core import PydanticUndefined, 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. from .warnings import ArbitraryTypeWarning
  15. if sys.version_info < (3, 11):
  16. from typing_extensions import Protocol
  17. else:
  18. from typing import Protocol
  19. _inspect_validator = _decorators.inspect_validator
  20. @dataclasses.dataclass(frozen=True, **_internal_dataclass.slots_true)
  21. class AfterValidator:
  22. """!!! abstract "Usage Documentation"
  23. [field *after* validators](../concepts/validators.md#field-after-validator)
  24. A metadata class that indicates that a validation should be applied **after** the inner validation logic.
  25. Attributes:
  26. func: The validator function.
  27. Example:
  28. ```python
  29. from typing import Annotated
  30. from pydantic import AfterValidator, BaseModel, ValidationError
  31. MyInt = Annotated[int, AfterValidator(lambda v: v + 1)]
  32. class Model(BaseModel):
  33. a: MyInt
  34. print(Model(a=1).a)
  35. #> 2
  36. try:
  37. Model(a='a')
  38. except ValidationError as e:
  39. print(e.json(indent=2))
  40. '''
  41. [
  42. {
  43. "type": "int_parsing",
  44. "loc": [
  45. "a"
  46. ],
  47. "msg": "Input should be a valid integer, unable to parse string as an integer",
  48. "input": "a",
  49. "url": "https://errors.pydantic.dev/2/v/int_parsing"
  50. }
  51. ]
  52. '''
  53. ```
  54. """
  55. func: core_schema.NoInfoValidatorFunction | core_schema.WithInfoValidatorFunction
  56. def __get_pydantic_core_schema__(self, source_type: Any, handler: GetCoreSchemaHandler) -> core_schema.CoreSchema:
  57. schema = handler(source_type)
  58. info_arg = _inspect_validator(self.func, 'after')
  59. if info_arg:
  60. func = cast(core_schema.WithInfoValidatorFunction, self.func)
  61. return core_schema.with_info_after_validator_function(func, schema=schema)
  62. else:
  63. func = cast(core_schema.NoInfoValidatorFunction, self.func)
  64. return core_schema.no_info_after_validator_function(func, schema=schema)
  65. @classmethod
  66. def _from_decorator(cls, decorator: _decorators.Decorator[_decorators.FieldValidatorDecoratorInfo]) -> Self:
  67. return cls(func=decorator.func)
  68. @dataclasses.dataclass(frozen=True, **_internal_dataclass.slots_true)
  69. class BeforeValidator:
  70. """!!! abstract "Usage Documentation"
  71. [field *before* validators](../concepts/validators.md#field-before-validator)
  72. A metadata class that indicates that a validation should be applied **before** the inner validation logic.
  73. Attributes:
  74. func: The validator function.
  75. json_schema_input_type: The input type used to generate the appropriate
  76. JSON Schema (in validation mode). The actual input type is `Any`.
  77. Example:
  78. ```python
  79. from typing import Annotated
  80. from pydantic import BaseModel, BeforeValidator
  81. MyInt = Annotated[int, BeforeValidator(lambda v: v + 1)]
  82. class Model(BaseModel):
  83. a: MyInt
  84. print(Model(a=1).a)
  85. #> 2
  86. try:
  87. Model(a='a')
  88. except TypeError as e:
  89. print(e)
  90. #> can only concatenate str (not "int") to str
  91. ```
  92. """
  93. func: core_schema.NoInfoValidatorFunction | core_schema.WithInfoValidatorFunction
  94. json_schema_input_type: Any = PydanticUndefined
  95. def __get_pydantic_core_schema__(self, source_type: Any, handler: GetCoreSchemaHandler) -> core_schema.CoreSchema:
  96. schema = handler(source_type)
  97. input_schema = (
  98. None
  99. if self.json_schema_input_type is PydanticUndefined
  100. else handler.generate_schema(self.json_schema_input_type)
  101. )
  102. info_arg = _inspect_validator(self.func, 'before')
  103. if info_arg:
  104. func = cast(core_schema.WithInfoValidatorFunction, self.func)
  105. return core_schema.with_info_before_validator_function(
  106. func,
  107. schema=schema,
  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 used to generate the appropriate
  133. JSON Schema (in validation mode). The actual input type is `Any`.
  134. Example:
  135. ```python
  136. from typing import Annotated, Union
  137. from pydantic import BaseModel, PlainValidator
  138. def validate(v: object) -> int:
  139. if not isinstance(v, (int, str)):
  140. raise ValueError(f'Expected int or str, go {type(v)}')
  141. return int(v) + 1
  142. MyInt = Annotated[
  143. int,
  144. PlainValidator(validate, json_schema_input_type=Union[str, int]), # (1)!
  145. ]
  146. class Model(BaseModel):
  147. a: MyInt
  148. print(Model(a='1').a)
  149. #> 2
  150. print(Model(a=1).a)
  151. #> 2
  152. ```
  153. 1. In this example, we've specified the `json_schema_input_type` as `Union[str, int]` which indicates to the JSON schema
  154. generator that in validation mode, the input type for the `a` field can be either a [`str`][] or an [`int`][].
  155. """
  156. func: core_schema.NoInfoValidatorFunction | core_schema.WithInfoValidatorFunction
  157. json_schema_input_type: Any = Any
  158. def __get_pydantic_core_schema__(self, source_type: Any, handler: GetCoreSchemaHandler) -> core_schema.CoreSchema:
  159. # Note that for some valid uses of PlainValidator, it is not possible to generate a core schema for the
  160. # source_type, so calling `handler(source_type)` will error, which prevents us from generating a proper
  161. # serialization schema. To work around this for use cases that will not involve serialization, we simply
  162. # catch any PydanticSchemaGenerationError that may be raised while attempting to build the serialization schema
  163. # and abort any attempts to handle special serialization.
  164. from pydantic import PydanticSchemaGenerationError
  165. try:
  166. schema = handler(source_type)
  167. # TODO if `schema['serialization']` is one of `'include-exclude-dict/sequence',
  168. # schema validation will fail. That's why we use 'type ignore' comments below.
  169. serialization = schema.get(
  170. 'serialization',
  171. core_schema.wrap_serializer_function_ser_schema(
  172. function=lambda v, h: h(v),
  173. schema=schema,
  174. return_schema=handler.generate_schema(source_type),
  175. ),
  176. )
  177. except PydanticSchemaGenerationError:
  178. serialization = None
  179. input_schema = handler.generate_schema(self.json_schema_input_type)
  180. info_arg = _inspect_validator(self.func, 'plain')
  181. if info_arg:
  182. func = cast(core_schema.WithInfoValidatorFunction, self.func)
  183. return core_schema.with_info_plain_validator_function(
  184. func,
  185. serialization=serialization, # pyright: ignore[reportArgumentType]
  186. json_schema_input_schema=input_schema,
  187. )
  188. else:
  189. func = cast(core_schema.NoInfoValidatorFunction, self.func)
  190. return core_schema.no_info_plain_validator_function(
  191. func,
  192. serialization=serialization, # pyright: ignore[reportArgumentType]
  193. json_schema_input_schema=input_schema,
  194. )
  195. @classmethod
  196. def _from_decorator(cls, decorator: _decorators.Decorator[_decorators.FieldValidatorDecoratorInfo]) -> Self:
  197. return cls(
  198. func=decorator.func,
  199. json_schema_input_type=decorator.info.json_schema_input_type,
  200. )
  201. @dataclasses.dataclass(frozen=True, **_internal_dataclass.slots_true)
  202. class WrapValidator:
  203. """!!! abstract "Usage Documentation"
  204. [field *wrap* validators](../concepts/validators.md#field-wrap-validator)
  205. A metadata class that indicates that a validation should be applied **around** the inner validation logic.
  206. Attributes:
  207. func: The validator function.
  208. json_schema_input_type: The input type used to generate the appropriate
  209. JSON Schema (in validation mode). The actual input type is `Any`.
  210. ```python
  211. from datetime import datetime
  212. from typing import Annotated
  213. from pydantic import BaseModel, ValidationError, WrapValidator
  214. def validate_timestamp(v, handler):
  215. if v == 'now':
  216. # we don't want to bother with further validation, just return the new value
  217. return datetime.now()
  218. try:
  219. return handler(v)
  220. except ValidationError:
  221. # validation failed, in this case we want to return a default value
  222. return datetime(2000, 1, 1)
  223. MyTimestamp = Annotated[datetime, WrapValidator(validate_timestamp)]
  224. class Model(BaseModel):
  225. a: MyTimestamp
  226. print(Model(a='now').a)
  227. #> 2032-01-02 03:04:05.000006
  228. print(Model(a='invalid').a)
  229. #> 2000-01-01 00:00:00
  230. ```
  231. """
  232. func: core_schema.NoInfoWrapValidatorFunction | core_schema.WithInfoWrapValidatorFunction
  233. json_schema_input_type: Any = PydanticUndefined
  234. def __get_pydantic_core_schema__(self, source_type: Any, handler: GetCoreSchemaHandler) -> core_schema.CoreSchema:
  235. schema = handler(source_type)
  236. input_schema = (
  237. None
  238. if self.json_schema_input_type is PydanticUndefined
  239. else handler.generate_schema(self.json_schema_input_type)
  240. )
  241. info_arg = _inspect_validator(self.func, 'wrap')
  242. if info_arg:
  243. func = cast(core_schema.WithInfoWrapValidatorFunction, self.func)
  244. return core_schema.with_info_wrap_validator_function(
  245. func,
  246. schema=schema,
  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], /) -> 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[Any],
  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[Any],
  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[Any],
  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[Any]], _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 (except for *after* validators, which should be instance methods):
  572. if mode != 'after':
  573. f = _decorators.ensure_classmethod_based_on_signature(f)
  574. dec_info = _decorators.ModelValidatorDecoratorInfo(mode=mode)
  575. return _decorators.PydanticDescriptorProxy(f, dec_info)
  576. return dec
  577. AnyType = TypeVar('AnyType')
  578. if TYPE_CHECKING:
  579. # If we add configurable attributes to IsInstance, we'd probably need to stop hiding it from type checkers like this
  580. InstanceOf = Annotated[AnyType, ...] # `IsInstance[Sequence]` will be recognized by type checkers as `Sequence`
  581. else:
  582. @dataclasses.dataclass(**_internal_dataclass.slots_true)
  583. class InstanceOf:
  584. '''Generic type for annotating a type that is an instance of a given class.
  585. Example:
  586. ```python
  587. from pydantic import BaseModel, InstanceOf
  588. class Foo:
  589. ...
  590. class Bar(BaseModel):
  591. foo: InstanceOf[Foo]
  592. Bar(foo=Foo())
  593. try:
  594. Bar(foo=42)
  595. except ValidationError as e:
  596. print(e)
  597. """
  598. [
  599. │ {
  600. │ │ 'type': 'is_instance_of',
  601. │ │ 'loc': ('foo',),
  602. │ │ 'msg': 'Input should be an instance of Foo',
  603. │ │ 'input': 42,
  604. │ │ 'ctx': {'class': 'Foo'},
  605. │ │ 'url': 'https://errors.pydantic.dev/0.38.0/v/is_instance_of'
  606. │ }
  607. ]
  608. """
  609. ```
  610. '''
  611. @classmethod
  612. def __class_getitem__(cls, item: AnyType) -> AnyType:
  613. return Annotated[item, cls()]
  614. @classmethod
  615. def __get_pydantic_core_schema__(cls, source: Any, handler: GetCoreSchemaHandler) -> core_schema.CoreSchema:
  616. from pydantic import PydanticSchemaGenerationError
  617. # use the generic _origin_ as the second argument to isinstance when appropriate
  618. instance_of_schema = core_schema.is_instance_schema(_generics.get_origin(source) or source)
  619. try:
  620. # Try to generate the "standard" schema, which will be used when loading from JSON
  621. original_schema = handler(source)
  622. except PydanticSchemaGenerationError:
  623. # If that fails, just produce a schema that can validate from python
  624. return instance_of_schema
  625. else:
  626. # Use the "original" approach to serialization
  627. instance_of_schema['serialization'] = core_schema.wrap_serializer_function_ser_schema(
  628. function=lambda v, h: h(v), schema=original_schema
  629. )
  630. return core_schema.json_or_python_schema(python_schema=instance_of_schema, json_schema=original_schema)
  631. __hash__ = object.__hash__
  632. if TYPE_CHECKING:
  633. SkipValidation = Annotated[AnyType, ...] # SkipValidation[list[str]] will be treated by type checkers as list[str]
  634. else:
  635. @dataclasses.dataclass(**_internal_dataclass.slots_true)
  636. class SkipValidation:
  637. """If this is applied as an annotation (e.g., via `x: Annotated[int, SkipValidation]`), validation will be
  638. skipped. You can also use `SkipValidation[int]` as a shorthand for `Annotated[int, SkipValidation]`.
  639. This can be useful if you want to use a type annotation for documentation/IDE/type-checking purposes,
  640. and know that it is safe to skip validation for one or more of the fields.
  641. Because this converts the validation schema to `any_schema`, subsequent annotation-applied transformations
  642. may not have the expected effects. Therefore, when used, this annotation should generally be the final
  643. annotation applied to a type.
  644. """
  645. def __class_getitem__(cls, item: Any) -> Any:
  646. return Annotated[item, SkipValidation()]
  647. @classmethod
  648. def __get_pydantic_core_schema__(cls, source: Any, handler: GetCoreSchemaHandler) -> core_schema.CoreSchema:
  649. with warnings.catch_warnings():
  650. warnings.simplefilter('ignore', ArbitraryTypeWarning)
  651. original_schema = handler(source)
  652. metadata = {'pydantic_js_annotation_functions': [lambda _c, h: h(original_schema)]}
  653. return core_schema.any_schema(
  654. metadata=metadata,
  655. serialization=core_schema.wrap_serializer_function_ser_schema(
  656. function=lambda v, h: h(v), schema=original_schema
  657. ),
  658. )
  659. __hash__ = object.__hash__
  660. _FromTypeT = TypeVar('_FromTypeT')
  661. class ValidateAs:
  662. """A helper class to validate a custom type from a type that is natively supported by Pydantic.
  663. Args:
  664. from_type: The type natively supported by Pydantic to use to perform validation.
  665. instantiation_hook: A callable taking the validated type as an argument, and returning
  666. the populated custom type.
  667. Example:
  668. ```python {lint="skip"}
  669. from typing import Annotated
  670. from pydantic import BaseModel, TypeAdapter, ValidateAs
  671. class MyCls:
  672. def __init__(self, a: int) -> None:
  673. self.a = a
  674. def __repr__(self) -> str:
  675. return f"MyCls(a={self.a})"
  676. class Model(BaseModel):
  677. a: int
  678. ta = TypeAdapter(
  679. Annotated[MyCls, ValidateAs(Model, lambda v: MyCls(a=v.a))]
  680. )
  681. print(ta.validate_python({'a': 1}))
  682. #> MyCls(a=1)
  683. ```
  684. """
  685. # TODO: make use of PEP 747
  686. def __init__(self, from_type: type[_FromTypeT], /, instantiation_hook: Callable[[_FromTypeT], Any]) -> None:
  687. self.from_type = from_type
  688. self.instantiation_hook = instantiation_hook
  689. def __get_pydantic_core_schema__(self, source: Any, handler: GetCoreSchemaHandler) -> core_schema.CoreSchema:
  690. schema = handler(self.from_type)
  691. return core_schema.no_info_after_validator_function(
  692. self.instantiation_hook,
  693. schema=schema,
  694. )