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
| Name | Type | Description |
|---|---|---|
| schema | CoreSchema | The underlying schema to which the default value behavior will be applied. |
| default | Any = PydanticUndefined | The 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 | `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 |
Returns
| Type | Description |
|---|---|
WithDefaultSchema | A new schema dictionary of type 'default' wrapping the original schema with the specified default value logic. |