If a field do not set any value, then pydantic validator is not called on that field. You can customize this behaviour by passing the argument ‘always=True’.
validator_demo_6.py
from pydantic import BaseModel, ValidationError, validator
from typing import Optional
class Box(BaseModel):
id: int
weight: int = None
height: int = None
@validator('*', always=True)
def allValidator(cls, value):
if(value == None):
raise ValueError('id, weight and height must set to a value')
if(value < 1):
raise ValueError('id, weight and height must > 0')
return value
try:
b1 = Box(id = 1, weight = 0)
print(b1)
except ValidationError as e:
print(e.json())
Output
[
{
"loc": [
"weight"
],
"msg": "id, weight and height must > 0",
"type": "value_error"
},
{
"loc": [
"height"
],
"msg": "id, weight and height must set to a value",
"type": "value_error"
}
]
No comments:
Post a Comment