How do validators work in pydantic v2?
- User
How do validators work in pydantic v2?
Assistant
Here is a quick overview of how validators work in Pydantic v2:
-
Validators allow you to customize how data is validated beyond just type checking. They are methods that are called during validation.
-
There are root validators that are called on the whole data object, and field validators that are called per field.
-
Root validators are defined as methods with names starting with "validate_" on the model class. They receive "self" and the data dict as arguments.
-
Field validators are defined as methods with names starting with "validate_" on the FieldInfo class for that field. They receive "value", the field values dict, and "values" the whole data values dict.
-
Validators can raise ValidationError to indicate invalid data, or modify the values by returning the new value.
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.