# 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).

```python
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](https://www.ask-codey.com/glossary?category=fastapi&level=4)

Updated: November 17, 2026

## In other subjects

- [Custom Validator in Angular](https://www.ask-codey.com/glossary/custom-validator)

Source: https://www.ask-codey.com/glossary/custom-validator-fastapi
