Skip to main content

nullable_schema

Returns a schema that matches a nullable value, e.g.:

from pydantic_core import SchemaValidator, core_schema

schema = core_schema.nullable_schema(core_schema.str_schema())
v = SchemaValidator(schema)
assert v.validate_python(None) is None
def nullable_schema(
schema: CoreSchema,
strict: bool | None = None,
ref: str | None = None,
metadata: dict[str, Any] | None = None,
serialization: SerSchema | None = None
) - > NullableSchema

Returns a schema that matches a nullable value, e.g.:

from pydantic_core import SchemaValidator, core_schema

schema = core_schema.nullable_schema(core_schema.str_schema())
v = SchemaValidator(schema)
assert v.validate_python(None) is None

Parameters

NameTypeDescription
schemaCoreSchemaThe inner schema to wrap, defining the type that is allowed to be null.
strict`boolNone` = None
ref`strNone` = None
metadata`dict[str, Any]None` = None
serialization`SerSchemaNone` = None

Returns

TypeDescription
NullableSchemaA schema definition that allows the wrapped type to accept None values during validation.