tezvyn:

Enforce positive price and SKU format using Pydantic Field without custom validators

Source: pydantic.devintermediate

WHAT IT TESTS: Pydantic V2 Field constraints vs custom validators. ANSWER OUTLINE: Use Field(gt=0) for price and Field(pattern=r'^ITEM-\d{5}$') for SKU; mention Annotated. RED FLAG: Suggesting @field_validator or conint/constr.

WHAT IT TESTS: Whether you know Pydantic V2's native Field constraints and avoid unnecessary custom validators. ANSWER OUTLINE: First, set price: float = Field(gt=0) to enforce positivity via numeric constraints. Second, set sku: str = Field(pattern=r'^ITEM-\d{5}$') to enforce regex format. Third, mention that Annotated[str, Field(pattern=...)] works for type aliases. RED FLAG: Reaching for @field_validator, root_validator, or deprecated conint/constr types instead of built-in Field kwargs.

Read the original → pydantic.dev

Get five bites like this every day.

Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.

Enforce positive price and SKU format using Pydantic Field without custom validators · Tezvyn