Skip to main content

with_default_schema

Returns a schema that adds a default value to the given schema, e.g.:

from pydantic_core import SchemaValidator, core_schema

schema = core_schema.with_default_schema(core_schema.str_schema(), default='hello')
wrapper_schema = core_schema.typed_dict_schema(
{'a': core_schema.typed_dict_field(schema)}
)
v = SchemaValidator(wrapper_schema)
assert v.validate_python({}) == v.validate_python({'a': 'hello'})
def with_default_schema(
schema: CoreSchema,
default: Any = PydanticUndefined,
default_factory: Callable[[], Any] | Callable[[dict[str, Any]], Any] | None = null,
default_factory_takes_data: bool | None = null,
on_error: Literal['raise', 'omit', 'default'] | None = null,
validate_default: bool | None = null,
strict: bool | None = null,
ref: str | None = null,
metadata: dict[str, Any] | None = null,
serialization: SerSchema | None = null
) - > WithDefaultSchema

Returns a schema that adds a default value to the given schema.

Parameters

NameTypeDescription
schemaCoreSchemaThe underlying schema to which the default value behavior will be applied.
defaultAny = PydanticUndefinedThe static default value to use when the input data is missing a value for this schema.
default_factory`Callable[[], Any]Callable[[dict[str, Any]], Any]
default_factory_takes_data`boolNone` = null
on_error`Literal['raise', 'omit', 'default']None` = null
validate_default`boolNone` = null
strict`boolNone` = null
ref`strNone` = null
metadata`dict[str, Any]None` = null
serialization`SerSchemaNone` = null

Returns

TypeDescription
WithDefaultSchemaA new schema dictionary of type 'default' wrapping the original schema with the specified default value logic.