Complete worked example
python
from pydantic import BaseModel, ConfigDict, Field, ValidationError
class Limits(BaseModel):
model_config = ConfigDict(extra="ignore")
count: int = Field(ge=1, le=10)
def inspect_example(payload):
current = Limits(count=2)
raw = current.model_dump()
raw.update(payload)
try:
updated = Limits.model_validate(raw)
return {"value": updated.model_dump(), "original": current.model_dump()}
except ValidationError as error:
return {"error": sorted({e["type"] for e in error.errors()})}- Reasoning
- current.model_dump creates the baseline mapping and raw.update adds the proposed field values to that separate mapping. The original model is retained for comparison. A numeric string can be normalized by the declared integer policy; zero and an excessive count are rejected rather than being copied into an apparently valid Limits instance. The inspection result includes both the validated next value and the unchanged original value. The lab also checks that the input mapping was not mutated. These observations separate a successful validated update from accidentally modifying either the caller data or the previously accepted state.