!!! abstract "Usage Documentation" [field *after* validators](../concepts/validators.md#field-after-validator) A metadata class that indicates that a validation should be applied **after** the inner validation logic. Attributes: func: The validator function. Example:
| 27 | |
| 28 | @dataclasses.dataclass(frozen=True, **_internal_dataclass.slots_true) |
| 29 | class AfterValidator: |
| 30 | class="st">""class="st">"!!! abstract "Usage Documentation" |
| 31 | [field *after* validators](../concepts/validators.mdclass="cm">#field-after-validator) |
| 32 | |
| 33 | A metadata class that indicates that a validation should be applied **after** the inner validation logic. |
| 34 | |
| 35 | Attributes: |
| 36 | func: The validator function. |
| 37 | |
| 38 | Example: |
| 39 | ```python |
| 40 | from typing import Annotated |
| 41 | |
| 42 | from pydantic import AfterValidator, BaseModel, ValidationError |
| 43 | |
| 44 | MyInt = Annotated[int, AfterValidator(lambda v: v + 1)] |
| 45 | |
| 46 | class Model(BaseModel): |
| 47 | a: MyInt |
| 48 | |
| 49 | print(Model(a=1).a) |
| 50 | class="cm">#> 2 |
| 51 | |
| 52 | try: |
| 53 | Model(a=&class="cm">#x27;a') |
| 54 | except ValidationError as e: |
| 55 | print(e.json(indent=2)) |
| 56 | &class="cm">#x27;class="st">'' |
| 57 | [ |
| 58 | { |
| 59 | class="st">"type": class="st">"int_parsing", |
| 60 | class="st">"loc": [ |
| 61 | class="st">"a" |
| 62 | ], |
| 63 | class="st">"msg": class="st">"Input should be a valid integer, unable to parse string as an integer", |
| 64 | class="st">"input": class="st">"a", |
| 65 | class="st">"url": class="st">"https://errors.pydantic.dev/2/v/int_parsing" |
| 66 | } |
| 67 | ] |
| 68 | &class="cm">#x27;class="st">'' |
| 69 | ``` |
| 70 | class="st">""" |
| 71 | |
| 72 | func: core_schema.NoInfoValidatorFunction | core_schema.WithInfoValidatorFunction |
| 73 | |
| 74 | def __get_pydantic_core_schema__(self, source_type: Any, handler: GetCoreSchemaHandler) -> core_schema.CoreSchema: |
| 75 | schema = handler(source_type) |
| 76 | info_arg = _inspect_validator(self.func, mode=&class="cm">#x27;afterclass="st">', type='field') |
| 77 | if info_arg: |
| 78 | func = cast(core_schema.WithInfoValidatorFunction, self.func) |
| 79 | return core_schema.with_info_after_validator_function(func, schema=schema) |
| 80 | else: |
| 81 | func = cast(core_schema.NoInfoValidatorFunction, self.func) |
| 82 | return core_schema.no_info_after_validator_function(func, schema=schema) |
| 83 | |
| 84 | @classmethod |
| 85 | def _from_decorator(cls, decorator: _decorators.Decorator[_decorators.FieldValidatorDecoratorInfo]) -> Self: |
| 86 | return cls(func=decorator.func) |
no outgoing calls