new·The score now tells you which way it movedA brain's exam only ever grows: its own material writes questions, and so does every question a real caller asked and did not get answered. The score is a percentage over that growing set, so a brain that learned more could post a smaller number — and this week three did. One of them answered two MORE questions than the week before and showed eighteen points less. Printed as a single percentage, that reads as decline to a reader and as punishment to anyone who contributes material.all news →
mozg.beta
Sign in

Pydantic · API reference · all subjects

functional_validators

6 notes, read out of this brain and free to use. Each one was extracted from a source and is re-checked against its exam.

field_validator decorator signature

The field_validator decorator is imported from pydantic.functional_validators. It is used to define custom validation functions for Pydantic model fields. The decorator takes the field names to validate as arguments and can be applied to methods that perform validation logic.

model_validator decorator signature

The model_validator decorator is imported from pydantic.functional_validators. It is used to define custom validation functions that operate on the entire model or root-level data, rather than individual fields.

Pydantic functional_validators module

The pydantic.functional_validators module contains decorators for implementing custom validation logic in Pydantic models. It provides field_validator for field-level validation and model_validator for model-level validation.

Prefer after validators over before validators

Use after validators rather than before validators when possible, because they run after Pydantic validation and guarantee working with the correct field type. Before validators receive arbitrary input data, making them more error-prone. Model validators using before mode receive input that isn't necessarily a dict—it can be an arbitrary object.

Annotated pattern preferred for validators

Prefer using the annotated pattern with AfterValidator for validators (field: Annotated[int, AfterValidator(is_even)]) over @field_validator decorators. This places the validator logic next to the field definition, making behavior clear. When using @field_validator decorators, define them as classmethods and be aware that decorator order and interaction with subclasses can cause unclear behavior.

AfterValidator and field_validator decorator example

Example mixing annotated and decorator validators: from pydantic import BaseModel, field_validator; def is_even(value: int) -> int: if value % 2 == 1: raise ValueError(f'{value} is not an even number'); return value; class Model(BaseModel): even: Annotated[int, AfterValidator(is_even)]; odd: int; @field_validator('odd', mode='after'); @classmethod; def is_odd(cls, value: int) -> int: if value % 2 == 0: raise ValueError(f'{value} is not an odd number'); return value. The annotated pattern is preferred for clarity.

Give your agent this brain