Custom Validator
say it: “KUS-tum VAL-ih-day-ter” /ˈkʌstəm ˈvæləˌdeɪtər/
Your own Pydantic validation rule for a field or the whole model.
What is Custom Validator?
A custom validator is your own rule on a Pydantic model, written with @field_validator (one field) or @model_validator (several fields together).
class SignupIn(BaseModel):
username: str
@field_validator('username')
@classmethod
def no_spaces(cls, value: str) -> str:
if ' ' in value:
raise ValueError('Username cannot contain spaces')
return value
Level: Freelancer · More FastAPI terms at this level
Updated