Skip to content
Sarj Code Standards

no-duplicate-field-validator-body

Pydantic field validators in one model repeat the same body.

Why

Two field validators with the same mode and the same body are one validation rule written twice; a later fix to one copy silently leaves the other field validated by the old rule.

Fix

List every field in one @field_validator(...) decorator and branch on info.field_name where the fields genuinely differ.

Examples

Before — flagged Two validators repeat one strip rule
models/contact.py
from pydantic import BaseModel, field_validator
class Contact(BaseModel):
first_name: str
last_name: str
@field_validator("first_name", mode="before")
@classmethod
def strip_first(cls, value: str) -> str:
return value.strip()
@field_validator("last_name", mode="before")
@classmethod
def strip_last(cls, raw: str) -> str:
return raw.strip()
After — preferred One validator names every field that shares the rule
models/contact.py
from pydantic import BaseModel, field_validator
class Contact(BaseModel):
first_name: str
last_name: str
@field_validator("first_name", "last_name", mode="before")
@classmethod
def strip_names(cls, value: str) -> str:
return value.strip()