wuelion Stack Overflow • Recommends
On “How to validate more than one field of a Pydantic model?”
To extend on the answer of Rahul R , this example shows in more detail how to use the pydantic validators.
This example contains all the necessary information to answer your question.
Note, that there is also the option to use a @root_validator , as mentioned by Kentgrav , see the example at the bottom of the post for more details.
import pydantic
class Parent(pydantic.BaseModel):
name: str
comments: str
class Customer(Parent):
address: str
phone: str
# If you want to apply the Validator to the fields "name", "comments", "address", "phone"
@pydantic.validator("name", "comments", "address", "phone")
@classmethod
def validate_all_fields_one_by_one(cls, field_value):
# Do the…