Core Schema and Validator Data Model
The data model of pydantic-core revolves around the translation of a CoreSchema (a Python-side definition) into high-performance Rust-based validators and serializers.
Key Components:
- CoreSchema: A recursive, union-based data structure (implemented as
TypedDictin Python) that defines the validation and serialization logic for a specific type. It includes fields liketype,ref, andmetadata. - SchemaValidator: The primary engine for validation. It is initialized from a
CoreSchemaand aCoreConfig. Internally, it holds aCombinedValidator, which is an enum of specialized validator implementations (e.g.,IntValidator,ModelValidator). - SchemaSerializer: The engine for converting Python objects into JSON or other Python formats. Like the validator, it is built from a
CoreSchemaand uses aCombinedSerializerenum. - Input: A Rust trait that abstracts over different input formats, primarily
PythonInput(wrappingPyAny) andJsonInput(wrappingjiter::JsonValue). - ValidationError: An exception raised when validation fails. It encapsulates one or more
PyLineErrorobjects, each detailing theErrorType, theLocation(path) of the error, and theinput_valuethat caused it. - SchemaError: An exception raised during the construction of a
SchemaValidatororSchemaSerializerif the providedCoreSchemais invalid or inconsistent. - CoreConfig: A configuration object that provides global settings such as
strictmode,extra_fields_behavior, and string constraints.
The diagram illustrates how the CoreSchema acts as the source of truth for both validation and serialization, and how these processes interact with input data and error reporting.
Key Architectural Findings:
- CoreSchema is a recursive TypedDict union that serves as the blueprint for both validation and serialization.
- SchemaValidator and SchemaSerializer are the core Rust-implemented classes exposed to Python.
- The Input trait allows the same validation logic to be applied to both Python objects and raw JSON data.
- ValidationError is a structured container for PyLineError, which provides detailed context (type, location, input) for each failure.
- SchemaError handles errors in the schema definition itself, separate from data validation errors.
Loading diagram...