MCPcopy
hub / github.com/pydantic/pydantic / str_schema

Function str_schema

pydantic-core/python/pydantic_core/core_schema.py:889–948  ·  pydantic-core/python/pydantic_core/core_schema.py::str_schema

Returns a schema that matches a string value, e.g.: ```py 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' ``` Args:

(
    *,
    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,
)

Source from the content-addressed store, hash-verified

887
888
889def str_schema(
890 *,
891 pattern: str | Pattern[str] | None = None,
892 max_length: int | None = None,
893 min_length: int | None = None,
894 strip_whitespace: bool | None = None,
895 to_lower: bool | None = None,
896 to_upper: bool | None = None,
897 regex_engine: Literal[&class="cm">#x27;rust-regexclass="st">', 'python-re'] | None = None,
898 strict: bool | None = None,
899 coerce_numbers_to_str: bool | None = None,
900 ref: str | None = None,
901 metadata: dict[str, Any] | None = None,
902 serialization: SerSchema | None = None,
903) -> StringSchema:
904 class="st">"""
905 Returns a schema that matches a string value, e.g.:
906
907 ```py
908 from pydantic_core import SchemaValidator, core_schema
909
910 schema = core_schema.str_schema(max_length=10, min_length=2)
911 v = SchemaValidator(schema)
912 assert v.validate_python(&class="cm">#x27;helloclass="st">') == 'hello'
913 ```
914
915 Args:
916 pattern: A regex pattern that the value must match
917 max_length: The value must be at most this length
918 min_length: The value must be at least this length
919 strip_whitespace: Whether to strip whitespace from the value
920 to_lower: Whether to convert the value to lowercase
921 to_upper: Whether to convert the value to uppercase
922 regex_engine: The regex engine to use for pattern validation. Default is &class="cm">#x27;rust-regex'.
923 - `rust-regex` uses the [`regex`](https://docs.rs/regex) Rust
924 crate, which is non-backtracking and therefore more DDoS
925 resistant, but does not support all regex features.
926 - `python-re` use the [`re`](https://docs.python.org/3/library/re.html) module,
927 which supports all regex features, but may be slower.
928 strict: Whether the value should be a string or a value that can be converted to a string
929 coerce_numbers_to_str: Whether to enable coercion of any `Number` type to `str` (not applicable in `strict` mode).
930 ref: optional unique identifier of the schema, used to reference the schema in other places
931 metadata: Any other information you want to include with the schema, not used by pydantic-core
932 serialization: Custom serialization schema
933 class="st">"""
934 return _dict_not_none(
935 type=&class="cm">#x27;str',
936 pattern=pattern,
937 max_length=max_length,
938 min_length=min_length,
939 strip_whitespace=strip_whitespace,
940 to_lower=to_lower,
941 to_upper=to_upper,
942 regex_engine=regex_engine,
943 strict=strict,
944 coerce_numbers_to_str=coerce_numbers_to_str,
945 ref=ref,
946 metadata=metadata,

Callers

nothing calls this directly

Calls 1

_dict_not_noneFunction · 0.85

Tested by

no test coverage detected