0

I am using Pydantic to validate my payload. I have created a class like this -

class PayloadValidator(BaseModel):
    """
    This model validates the payload structure and each attribute of payload.
    """

    tenantId: str
    emailId: List[str]
    role: str
    # means the payload emails are duplicate.
    _duplicates: bool = False
    # means the emails in payload are already used for the requesting tenants.
    _existing:  bool = False
    # means the emails in payload are non corporate emails.
    _non_corp_emails: bool = False
    # final invite list
    _valid_emails = List[str]

I want to perform certain validations on emailId. So I wrote couple of methods -

def email_format(a):
    """
    raise an exception or return a tuple.
    """
    pass

def email_existance(a,b,c):
    """
    raise an exception or return a tuple.
    """
    pass

def duplicate_emails(a):
    """
    raise an exception or return a tuple.
    """
    pass

Now these function are in a different module and I want to sequentially use it in __get__validator__(cls) method of Pydantic.

The documentation says that I have to use yeild. How can I make it work with my use case?

Jeet Patel
  • 1,140
  • 18
  • 51

1 Answers1

0

you can do something like below lets say u want to validate payload for email_format method

from pydantic.validators import str_validator
class EmailValidation(str):
       @classmethod
       def __get_validators__(self):
           yield str_validator
           yield email_format

similarly u can validate other payloads too

Amit Tiwari
  • 163
  • 2
  • 10