Skip to content
You're viewing docs for v2.6. See the latest version →

Pydantic Plugins

Usage docs: https://docs.pydantic.dev/2.6/concepts/plugins#build-a-plugin

Plugin interface for Pydantic plugins, and related types.

SchemaTypePath

Bases: NamedTuple

Path defining where schema_type was defined, or where TypeAdapter was called.

Attributes

module

Type: str

name

Type: str


PydanticPluginProtocol

Bases: Protocol

Protocol defining the interface for Pydantic plugins.

Methods

new_schema_validator

def new_schema_validator(
    schema: CoreSchema,
    schema_type: Any,
    schema_type_path: SchemaTypePath,
    schema_kind: SchemaKind,
    config: CoreConfig | None,
    plugin_settings: dict[str, object],
) -> tuple[ValidatePythonHandlerProtocol | None, ValidateJsonHandlerProtocol | None, ValidateStringsHandlerProtocol | None]

This method is called for each plugin every time a new SchemaValidator is created.

It should return an event handler for each of the three validation methods, or None if the plugin does not implement that method.

Returns

tuple[ValidatePythonHandlerProtocol | None, ValidateJsonHandlerProtocol | None, ValidateStringsHandlerProtocol | None] — A tuple of optional event handlers for each of the three validation methods - validate_python, validate_json, validate_strings.

Parameters

schema : CoreSchema

The schema to validate against.

schema_type : Any

The original type which the schema was created from, e.g. the model class.

schema_type_path : SchemaTypePath

Path defining where schema_type was defined, or where TypeAdapter was called.

schema_kind : SchemaKind

The kind of schema to validate against.

config : CoreConfig | None

The config to use for validation.

plugin_settings : dict[str, object]

Any plugin settings.


BaseValidateHandlerProtocol

Bases: Protocol

Base class for plugin callbacks protocols.

You shouldn’t implement this protocol directly, instead use one of the subclasses with adds the correctly typed on_error method.

Attributes

on_enter

on_enter is changed to be more specific on all subclasses

Type: Callable[..., None]

Methods

on_success

def on_success(result: Any) -> None

Callback to be notified of successful validation.

Returns

None

Parameters

result : Any

The result of the validation.

on_error

def on_error(error: ValidationError) -> None

Callback to be notified of validation errors.

Returns

None

Parameters

error : ValidationError

The validation error.

on_exception

def on_exception(exception: Exception) -> None

Callback to be notified of validation exceptions.

Returns

None

Parameters

exception : Exception

The exception raised during validation.


ValidatePythonHandlerProtocol

Bases: BaseValidateHandlerProtocol, Protocol

Event handler for SchemaValidator.validate_python.

Methods

on_enter

def on_enter(
    input: Any,
    strict: bool | None = None,
    from_attributes: bool | None = None,
    context: dict[str, Any] | None = None,
    self_instance: Any | None = None,
) -> None

Callback to be notified of validation start, and create an instance of the event handler.

Returns

None

Parameters

input : Any

The input to be validated.

strict : bool | None Default: None

Whether to validate the object in strict mode.

from_attributes : bool | None Default: None

Whether to validate objects as inputs by extracting attributes.

context : dict[str, Any] | None Default: None

The context to use for validation, this is passed to functional validators.

self_instance : Any | None Default: None

An instance of a model to set attributes on from validation, this is used when running validation from the __init__ method of a model.


ValidateJsonHandlerProtocol

Bases: BaseValidateHandlerProtocol, Protocol

Event handler for SchemaValidator.validate_json.

Methods

on_enter

def on_enter(
    input: str | bytes | bytearray,
    strict: bool | None = None,
    context: dict[str, Any] | None = None,
    self_instance: Any | None = None,
) -> None

Callback to be notified of validation start, and create an instance of the event handler.

Returns

None

Parameters

input : str | bytes | bytearray

The JSON data to be validated.

strict : bool | None Default: None

Whether to validate the object in strict mode.

context : dict[str, Any] | None Default: None

The context to use for validation, this is passed to functional validators.

self_instance : Any | None Default: None

An instance of a model to set attributes on from validation, this is used when running validation from the __init__ method of a model.


ValidateStringsHandlerProtocol

Bases: BaseValidateHandlerProtocol, Protocol

Event handler for SchemaValidator.validate_strings.

Methods

on_enter

def on_enter(
    input: StringInput,
    strict: bool | None = None,
    context: dict[str, Any] | None = None,
) -> None

Callback to be notified of validation start, and create an instance of the event handler.

Returns

None

Parameters

input : StringInput

The string data to be validated.

strict : bool | None Default: None

Whether to validate the object in strict mode.

context : dict[str, Any] | None Default: None

The context to use for validation, this is passed to functional validators.


NewSchemaReturns

Type: TypeAlias Default: 'tuple[ValidatePythonHandlerProtocol | None, ValidateJsonHandlerProtocol | None, ValidateStringsHandlerProtocol | None]'

SchemaKind

Type: TypeAlias Default: Literal['BaseModel', 'TypeAdapter', 'dataclass', 'create_model', 'validate_call']

StringInput

Type: TypeAlias Default: 'dict[str, StringInput]'