Skip to main content

dataclass_schema

Returns a schema for a dataclass. As with ModelSchema, this schema can only be used as a field within another schema, not as the root type.

def dataclass_schema(
cls: type[Any],
schema: CoreSchema,
fields: list[str],
generic_origin: type[Any] | None = None,
cls_name: str | None = None,
post_init: bool | None = None,
revalidate_instances: Literal['always', 'never', 'subclass-instances'] | None = None,
strict: bool | None = None,
ref: str | None = None,
metadata: dict[str, Any] | None = None,
serialization: SerSchema | None = None,
frozen: bool | None = None,
slots: bool | None = None,
config: CoreConfig | None = None
) - > DataclassSchema

Returns a schema for a dataclass. As with ModelSchema, this schema can only be used as a field within another schema, not as the root type.

Parameters

NameTypeDescription
clstype[Any]The dataclass type, used to perform subclass checks
schemaCoreSchemaThe schema to use for the dataclass fields
fieldslist[str]Fields of the dataclass, this is used in serialization and in validation during re-validation and while validating assignment
generic_origin`type[Any]None` = None
cls_name`strNone` = None
post_init`boolNone` = None
revalidate_instances`Literal['always', 'never', 'subclass-instances']None` = None
strict`boolNone` = None
ref`strNone` = None
metadata`dict[str, Any]None` = None
serialization`SerSchemaNone` = None
frozen`boolNone` = None
slots`boolNone` = None
config`CoreConfigNone` = None

Returns

TypeDescription
DataclassSchemaA dictionary-based schema definition for a dataclass, compatible with pydantic-core validation and serialization.