MCPcopy
hub / github.com/pydantic/pydantic / StringConstraints

Class StringConstraints

pydantic/types.py:693–749  ·  view source on GitHub ↗

!!! abstract "Usage Documentation" [String types](./standard_library_types.md#strings) A field metadata class to apply constraints to `str` types. Use this class as an annotation via [`Annotated`](https://docs.python.org/3/library/typing.html#typing.Annotated), as seen below. A

Source from the content-addressed store, hash-verified

691
692@_dataclasses.dataclass(frozen=True)
693class StringConstraints(annotated_types.GroupedMetadata):
694 """!!! abstract "Usage Documentation"
695 [String types](./standard_library_types.md#strings)
696
697 A field metadata class to apply constraints to `str` types.
698 Use this class as an annotation via [`Annotated`](https://docs.python.org/3/library/typing.html#typing.Annotated), as seen below.
699
700 Attributes:
701 strip_whitespace: Whether to remove leading and trailing whitespace.
702 to_upper: Whether to convert the string to uppercase.
703 to_lower: Whether to convert the string to lowercase.
704 strict: Whether to validate the string in strict mode.
705 min_length: The minimum length of the string.
706 max_length: The maximum length of the string.
707 pattern: A regex pattern that the string must match.
708 ascii_only: Whether the string should contain only ASCII characters.
709
710 Example:
711 ```python
712 from typing import Annotated
713
714 from pydantic.types import StringConstraints
715
716 ConstrainedStr = Annotated[str, StringConstraints(min_length=1, max_length=10)]
717 ```
718 """
719
720 strip_whitespace: bool | None = None
721 to_upper: bool | None = None
722 to_lower: bool | None = None
723 strict: bool | None = None
724 min_length: int | None = None
725 max_length: int | None = None
726 pattern: str | Pattern[str] | None = None
727 ascii_only: bool | None = None
728
729 def __iter__(self) -> Iterator[BaseMetadata]:
730 if self.min_length is not None:
731 yield MinLen(self.min_length)
732 if self.max_length is not None:
733 yield MaxLen(self.max_length)
734 if self.strict is not None:
735 yield Strict(self.strict)
736 if (
737 self.strip_whitespace is not None
738 or self.pattern is not None
739 or self.to_lower is not None
740 or self.to_upper is not None
741 or self.ascii_only is not None
742 ):
743 yield _fields.pydantic_general_metadata(
744 strip_whitespace=self.strip_whitespace,
745 to_upper=self.to_upper,
746 to_lower=self.to_lower,
747 pattern=self.pattern,
748 ascii_only=self.ascii_only,
749 )
750

Callers 9

MyModelClass · 0.90
ModelClass · 0.90
test_string_constraintsFunction · 0.90
ModelClass · 0.90
FooClass · 0.90
AddressClass · 0.90
BlogPostClass · 0.90
constrFunction · 0.85

Calls

no outgoing calls

Tested by 2

test_string_constraintsFunction · 0.72