Skip to main content

Core Schema Data Model

The Core Schema Data Model diagram illustrates the structure of pydantic-core's schema definitions, which are used to configure both validation and serialization.

At the center is the CoreSchema, which is a union of various schema types. The diagram highlights several key schema types:

  • TypedDictSchema: Represents a dictionary with a fixed set of keys. It contains a mapping of field names to TypedDictField objects.
  • TypedDictField: Defines the schema for an individual field within a TypedDict, including its own CoreSchema, whether it's required, and its aliases.
  • ListSchema: Represents a sequence of items, all following a specific items_schema.
  • StringSchema: Represents string data with various constraints like length and regex patterns.

The diagram also shows how validation and serialization are integrated:

  • Validator: Represented by various function-based schemas (e.g., BeforeValidatorFunctionSchema), which wrap a CoreSchema and apply a custom validation function.
  • Serializer: Represented by Specialized Serialization Schemas, which can be attached to almost any CoreSchema to customize how that data is serialized.
  • CoreConfig: Provides global configuration settings (like strict mode or extra_fields_behavior) that apply to the entire schema or specific sub-schemas.

Finally, the SchemaValidator and SchemaSerializer are the primary entry points of the library. They consume a CoreSchema and a CoreConfig to perform their respective operations on input data.

Key Architectural Findings:

  • CoreSchema is a comprehensive TypeAlias union of over 40 different schema types, each represented as a TypedDict.
  • TypedDictSchema and ListSchema are recursive, containing other CoreSchema instances via fields or items_schema.
  • Validation logic is often 'wrapped' around a schema using ValidatorFunctionSchema types (Before, After, Wrap, Plain).
  • Serialization behavior is customizable per-schema through an optional 'serialization' field of type SerSchema.
  • SchemaValidator and SchemaSerializer are the core Rust-backed classes that interpret these schema definitions.
Loading diagram...