from pydantic import BaseModel, EmailStr
class LoginRequest(BaseModel):
client_id: str
username: str | None = None
email: EmailStr | None = None
phone: str | None = None
password: str
from pydantic import BaseModel, ValidationError, field_validator
class Model(BaseModel):
x: int
@model_validator(mode='before')
@classmethod
def validate_x(cls, v: int) -> int:
if x is None:
raise ValueError('X is None')
return cls
try:
Model(x=None)
except ValidationError as e:
print(e)
"""X is None"""
class LoginRequest(BaseModel):
client_id: str
username: str | None = None
email: EmailStr | None = None
phone: str | None = None
password: str
@model_validator(mode='after')
def check_login_data_none(self) -> 'LoginRequest':
username = self.username
email = self.email
phone = self.phone
if username == email == phone == None:
raise ValueError('At least one login field should be not None')
return self