3.5. Pydantic Types Constrained
Source: https://pydantic-docs.helpmanual.io/usage/types/#constrained-types
conbytes
- type method for constraining bytescondecimal
- type method for constraining Decimalsconfloat
- type method for constraining floatsconfrozenset
- type method for constraining frozen setsconint
- type method for constraining intsconlist
- type method for constraining listsconset
- type method for constraining setsconstr
- type method for constraining strs
The value of numerous common types can be restricted using con*
type functions.
3.5.1. Arguments to conbytes
type method for constraining bytes
strip_whitespace: bool = False
: removes leading and trailing whitespaceto_lower: bool = False
: turns all characters to lowercasemin_length: int = None
: minimum length of the byte stringmax_length: int = None
: maximum length of the byte stringstrict: bool = False
: controls type coercion
3.5.2. Arguments to condecimal
type method for constraining Decimals
gt: Decimal = None
: enforces decimal to be greater than the set valuege: Decimal = None
: enforces decimal to be greater than or equal to the set valuelt: Decimal = None
: enforces decimal to be less than the set valuele: Decimal = None
: enforces decimal to be less than or equal to the set valuemax_digits: int = None
: maximum number of digits within the decimal. it does not include a zero before the decimal point or trailing decimal zeroesdecimal_places: int = None
: max number of decimal places allowed. it does not include trailing decimal zeroesmultiple_of: Decimal = None
: enforces decimal to be a multiple of the set value
3.5.3. Arguments to confloat
type method for constraining floats
strict: bool = False
: controls type coerciongt: float = None
: enforces float to be greater than the set valuege: float = None
: enforces float to be greater than or equal to the set valuelt: float = None
: enforces float to be less than the set valuele: float = None
: enforces float to be less than or equal to the set valuemultiple_of: float = None
: enforces float to be a multiple of the set value
3.5.4. Arguments to confrozenset
type method for constraining frozen sets
item_type: type[T]
: type of the frozenset itemsmin_items: int = None
: minimum number of items in the frozensetmax_items: int = None
: maximum number of items in the frozenset
3.5.5. Arguments to conint
type method for constraining ints
strict: bool = False
: controls type coerciongt: int = None
: enforces integer to be greater than the set valuege: int = None
: enforces integer to be greater than or equal to the set valuelt: int = None
: enforces integer to be less than the set valuele: int = None
: enforces integer to be less than or equal to the set valuemultiple_of: int = None
: enforces integer to be a multiple of the set value
3.5.6. Arguments to conlist
type method for constraining lists
item_type: type[T]
: type of the list itemsmin_items: int = None
: minimum number of items in the listmax_items: int = None
: maximum number of items in the listunique_items: bool = None
: enforces list elements to be unique
3.5.7. Arguments to conset
type method for constraining sets
item_type: type[T]
: type of the set itemsmin_items: int = None
: minimum number of items in the setmax_items: int = None
: maximum number of items in the set
3.5.8. Arguments to constr
type method for constraining strs
strip_whitespace: bool = False
: removes leading and trailing whitespaceto_lower: bool = False
: turns all characters to lowercasestrict: bool = False
: controls type coercionmin_length: int = None
: minimum length of the stringmax_length: int = None
: maximum length of the stringcurtail_length: int = None
: shrinks the string length to the set value when it is longer than the set valueregex: str = None
: regex to validate the string against
3.5.9. Example
>>>
... from decimal import Decimal
...
... from pydantic import (
... BaseModel,
... NegativeFloat,
... NegativeInt,
... PositiveFloat,
... PositiveInt,
... NonNegativeFloat,
... NonNegativeInt,
... NonPositiveFloat,
... NonPositiveInt,
... conbytes,
... condecimal,
... confloat,
... conint,
... conlist,
... conset,
... constr,
... Field,
... )
...
...
... class Model(BaseModel):
... lower_bytes: conbytes(to_lower=True)
... short_bytes: conbytes(min_length=2, max_length=10)
... strip_bytes: conbytes(strip_whitespace=True)
...
... lower_str: constr(to_lower=True)
... short_str: constr(min_length=2, max_length=10)
... regex_str: constr(regex=r'^apple (pie|tart|sandwich)$')
... strip_str: constr(strip_whitespace=True)
...
... big_int: conint(gt=1000, lt=1024)
... mod_int: conint(multiple_of=5)
... pos_int: PositiveInt
... neg_int: NegativeInt
... non_neg_int: NonNegativeInt
... non_pos_int: NonPositiveInt
...
... big_float: confloat(gt=1000, lt=1024)
... unit_interval: confloat(ge=0, le=1)
... mod_float: confloat(multiple_of=0.5)
... pos_float: PositiveFloat
... neg_float: NegativeFloat
... non_neg_float: NonNegativeFloat
... non_pos_float: NonPositiveFloat
...
... short_list: conlist(int, min_items=1, max_items=4)
... short_set: conset(int, min_items=1, max_items=4)
...
... decimal_positive: condecimal(gt=0)
... decimal_negative: condecimal(lt=0)
... decimal_max_digits_and_places: condecimal(max_digits=2, decimal_places=2)
... mod_decimal: condecimal(multiple_of=Decimal('0.25'))
...
... bigger_int: int = Field(..., gt=10000)