Reputation: 787
I am developing a FastAPI application. I have with the following schema
class Address(BaseModel):
address_string: str = Field(None)
address_street: str = Field(None)
addres_number: str = Field(None)
I like to have the field address_string conditionally required if address_street and addres_number are not present, and vice-versa, address_street and address_number are required if address_street is not present.
Currently I manage this by making all fields optional and using a root_validator to check the consistency, and documenting this conditional requirement in the description of the involved fields.
Is there a cleaner way to manage this built-in on FastAPI?
Upvotes: 5
Views: 5672
Reputation: 13269
Root validators, or validators on the optionally required fields are the beat solution.
Similar example on passwords here.
Upvotes: 3