1

I would like to do A>B validation when I have the following pydantic class, do you know how to do that?

class Test(BaseModel):
    a: int
    b: int
rikuto
  • 37
  • 1
  • 4

1 Answers1

2

You can use validator method from pydantic:

from pydantic import validator

class Test(BaseModel):
    a: int
    b: int

    @validator('b')
    def ab_validation(cls, b, values, **kwargs):
        if 'a' in values and b > values['a']:
            raise ValueError('B is greater than A')
        return b
fchancel
  • 2,396
  • 6
  • 21