Skip to main content

UnionSchema

This class defines the schema for union types, allowing a value to be validated against multiple potential sub-schemas. It supports different validation modes such as 'smart' or 'left-to-right' and provides options for automatic simplification of single-element unions and custom error reporting. The schema also includes standard configuration fields for strictness, references, and serialization behavior.

Attributes

AttributeTypeDescription
typeLiteral['union']A required literal string identifying the schema as a union type.
choices`list[CoreSchematuple[CoreSchema, str]]`
auto_collapsebool = trueWhether to automatically collapse unions with one element to the inner validator.
custom_error_typestrThe specific error type identifier to be raised when validation fails for all union choices.
custom_error_messagestrA user-defined error message string to override the default failure message.
custom_error_context`dict[str, strint
modeLiteral['smart', 'left_to_right'] = 'smart'Determines the validation strategy, either attempting to find the best match or processing choices sequentially.
strictboolWhether to enforce strict type checking and disallow data coercion during validation.
refstrAn optional identifier used for creating recursive schemas or referencing this schema elsewhere.
metadatadict[str, Any]A dictionary for storing arbitrary application-specific data that does not affect validation.
serializationSerSchemaConfiguration defining how the union should be handled during data serialization.

Methods


type()

def type() - > Literal['union']

Specifies the schema type as a union, indicating that the data must validate against one of several possible schemas.

Returns

TypeDescription
Literal['union']The literal string 'union' identifying this schema type.

choices()

def choices() - > list[CoreSchema | tuple[CoreSchema, str]]

Defines the list of possible schemas or tagged schemas that the input data can be validated against.

Returns

TypeDescription
`list[CoreSchematuple[CoreSchema, str]]`

auto_collapse()

def auto_collapse() - > bool

Determines whether to automatically simplify the union to its inner validator if only one choice is provided.

Returns

TypeDescription
boolA boolean flag indicating if auto-collapse is enabled; defaults to true.

custom_error_type()

def custom_error_type() - > str

Sets a specific error type identifier to be raised when validation fails for all union choices.

Returns

TypeDescription
strThe string identifier for the custom error type.

custom_error_message()

def custom_error_message() - > str

Provides a user-defined error message to be displayed when none of the union members match the input.

Returns

TypeDescription
strThe custom error message string.

custom_error_context()

def custom_error_context() - > dict[str, str | int | float]

Supplies additional metadata or variables to be interpolated into the custom error message.

Returns

TypeDescription
`dict[str, strint

mode()

def mode() - > Literal['smart', 'left_to_right']

Controls the validation strategy, choosing between 'smart' matching or 'left_to_right' sequential attempts.

Returns

TypeDescription
Literal['smart', 'left_to_right']The validation mode string; defaults to 'smart'.

strict()

def strict() - > bool

Enforces strict validation rules across all union choices, preventing type coercion during the matching process.

Returns

TypeDescription
boolA boolean indicating if strict mode is active.

ref()

def ref() - > str

Assigns a unique string identifier to the schema for use in recursive definitions or shared references.

Returns

TypeDescription
strThe unique reference string for this schema.

metadata()

def metadata() - > dict[str, Any]

Stores arbitrary application-specific data associated with the union schema that does not affect validation.

Returns

TypeDescription
dict[str, Any]A dictionary containing custom metadata fields.

serialization()

def serialization() - > SerSchema

Configures how the union should be processed during the serialization of data back to its raw format.

Returns

TypeDescription
SerSchemaThe serialization schema configuration object.