Skip to main content

str_schema

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

from pydantic_core import SchemaValidator, core_schema

schema = core_schema.str_schema(max_length=10, min_length=2)
v = SchemaValidator(schema)
assert v.validate_python('hello') == 'hello'
def str_schema(
pattern: str | Pattern[str] | None = None,
max_length: int | None = None,
min_length: int | None = None,
strip_whitespace: bool | None = None,
to_lower: bool | None = None,
to_upper: bool | None = None,
regex_engine: Literal['rust-regex', 'python-re'] | None = None,
strict: bool | None = None,
coerce_numbers_to_str: bool | None = None,
ref: str | None = None,
metadata: dict[str, Any] | None = None,
serialization: SerSchema | None = None
) - > StringSchema

Returns a schema that matches a string value.

Parameters

NameTypeDescription
pattern`strPattern[str]
max_length`intNone` = None
min_length`intNone` = None
strip_whitespace`boolNone` = None
to_lower`boolNone` = None
to_upper`boolNone` = None
regex_engine`Literal['rust-regex', 'python-re']None` = None
strict`boolNone` = None
coerce_numbers_to_str`boolNone` = None
ref`strNone` = None
metadata`dict[str, Any]None` = None
serialization`SerSchemaNone` = None

Returns

TypeDescription
StringSchemaA dictionary-based schema definition for string validation and transformation.