Skip to content

require-pydantic-ordinal-lower-bound

A Pydantic ordinal field maps its first position to N but accepts smaller integers.

Why

An unqualified N for the first ... mapping defines the origin of an ordinal field; matching schema metadata keeps runtime validation and generated JSON Schema aligned with that public contract.

Fix

Use PositiveInt for a 1-based ordinal, NonNegativeInt for a 0-based ordinal, or encode the origin with Field(ge=N) or equivalent top-level Annotated metadata.

Examples

Before — flagged An unqualified ordinal origin lacks a lower bound
api.py
from pydantic import BaseModel, Field
class CallDetail(BaseModel):
retry_attempt_number: int = Field(
default=1,
description="Which dial this call is within its retry group (1 for the first attempt).",
)
After — preferred An ordinal minimum is enforced
api.py
from pydantic import BaseModel, Field, PositiveInt
class CallDetail(BaseModel):
retry_attempt_number: PositiveInt = Field(
default=1,
description="Which dial this call is within its retry group (1 for the first attempt).",
)