Skip to content

no-nested-pydantic-field-validator

Outer-model Pydantic field validator is owned by a nested helper class.

Why

Pydantic collects validator metadata from the model class namespace. A decorator indented into a nested class is therefore invisible to the outer model, so its declared field silently loses validation.

Fix

Move the field-validator method into the outer model class.

Examples

Before — flagged Nested Config silently does not validate Settings.language
app/models.py
from pydantic import BaseModel, field_validator
class Settings(BaseModel):
language: str
class Config:
extra = "forbid"
@field_validator("language")
@classmethod
def normalize(cls, value):
return value.lower()
After — preferred Validator belongs to the model
app/models.py
from pydantic import BaseModel, field_validator
class Settings(BaseModel):
language: str
@field_validator("language")
@classmethod
def normalize(cls, value):
return value.lower()