‘conint’ function is used to constrain int data. You can following arguments to the conint function.
Argument |
Data type |
Description |
strict |
bool = False |
controls type coercion |
gt |
int = None |
enforces integer to be greater than the set value. |
ge |
int = None |
enforces integer to be greater than or equal to the set value |
lt |
int = None |
enforces integer to be less than the set value |
le |
int = None |
enforces integer to be less than or equal to the set value |
multiple_of |
int = None |
enforces integer to be a multiple of the set value |
Example
class Employee(BaseModel):
id: int
name: str
age: conint(gt = 18)
conint_demo_1.py
from pydantic import (
BaseModel,
conint,
ValidationError
)
class Employee(BaseModel):
id: int
name: str
age: conint(gt = 18)
try:
emp1 = Employee(id = 1, name = 'Krishna', age = 17)
except ValidationError as e:
print(e.json())
Output
[ { "loc": [ "age" ], "msg": "ensure this value is greater than 18", "type": "value_error.number.not_gt", "ctx": { "limit_value": 18 } } ]
No comments:
Post a Comment