I wrote a Pydantic model to validate API payload.
The payload has 2 attributes emailId
as list
and role
as str
{
"emailId": [],
"role":"Administrator"
}
I need to perform two validation on attribute email -
emailId
must not be empty.emailId
must not contain emails from x, y, z domains.
Hence to accomplish this I wrote 2 validation methods for emailId
as shown below -
class PayloadValidator(BaseModel):
emailId: List[str]
role: str
@validator("emailId")
def is_email_list_empty(cls, email):
if not email_id:
raise ValueError("Email list is empty.")
return email_id
@validator("emailId")
def valid_domains(cls, emailId):
pass
The problem here is that if the emailId
list is empty then the validators does not raise
ValueError
right away. It waits for all the validation method to finish execution and this is causing some serious problems to me.
Is there a way I can make it happen?