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
| Name | Type | Description |
|---|---|---|
| pattern | `str | Pattern[str] |
| 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 |
Returns
| Type | Description |
|---|---|
StringSchema | A dictionary-based schema definition for string validation and transformation. |