‘condecimal’ method is used to constrain decimal values. Below table summarizes the arguments of ‘condecimal’ method.
Argument |
Data type |
Description |
gt |
Decimal = None |
enforces Decimal to be greater than the set value. |
ge |
Decimal = None |
enforces Decimal to be greater than or equal to the set value |
lt |
Decimal = None |
enforces Decimal to be less than the set value |
le |
Decimal = None |
enforces Decimal to be less than or equal to the set value |
max_digits |
int = None |
maximum number of digits within the decimal. it does not include a zero before the decimal point or trailing decimal zeroes |
decimal_places |
int = None |
max number of decimal places allowed. it does not include trailing decimal zeroes |
multiple_of |
Decimal = None |
enforces decimal to be a multiple of the set value |
Find the below working application.
Example
class Box(BaseModel):
id: int
weight: condecimal(lt = 30, gt = 1, max_digits=4)
Find the below working application.
condecimal_demo_1.py
from pydantic import (
BaseModel,
condecimal,
ValidationError
)
class Box(BaseModel):
id: int
weight: condecimal(lt = 30, gt = 1, max_digits=4)
try:
box1 = Box(id = 1, weight = 10.456)
except ValidationError as e:
print(e.json())
Output
[ { "loc": [ "weight" ], "msg": "ensure that there are no more than 4 digits in total", "type": "value_error.decimal.max_digits", "ctx": { "max_digits": 4 } } ]
Previous Next Home
No comments:
Post a Comment