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

Pydantic V2 Field constraints vs custom validators.
Use Field(gt=0) for price and Field(pattern=r'^ITEM-\d{5}$') for SKU; mention Annotated.
Suggesting @field_validator or conint/constr.
WHAT THIS TESTS: This question checks if you know Pydantic V2's built-in Field constraints and can distinguish when native parameters suffice versus when a custom validator is actually necessary. Interviewers want to see awareness of gt, ge, lt, and le for numeric bounds and pattern for string regex, plus familiarity with modern Pydantic V2 patterns over legacy V1 approaches.
A GOOD ANSWER COVERS: First, the price constraint. You should mention price: float = Field(gt=0) because Pydantic Field accepts numeric comparison arguments including gt, ge, lt, and le. Second, the SKU format constraint. You should mention sku: str = Field(pattern=r'^ITEM-\d{5}$') because Field accepts a pattern parameter that applies regex validation directly without extra code. Third, the Annotated alternative. A strong candidate notes that from typing import Annotated allows reusable constraints like SKU = Annotated[str, Field(pattern=r'^ITEM-\d{5}$')], which keeps models clean and shareable. Fourth, V2 awareness. Mention that conint and constr are deprecated or discouraged in Pydantic V2 in favor of Field on standard types, showing migration knowledge and API fluency.
COMMON WRONG ANSWERS: Proposing @field_validator or @validator for either rule is a red flag because it ignores the built-in constraints and adds unnecessary boilerplate. Using conint(gt=0) or constr(pattern=...) reveals outdated Pydantic V1 knowledge. Suggesting JSON Schema extras like json_schema_extra to enforce runtime validation is incorrect because that only affects schema generation and does not validate data. Proposing pre-compiled regex objects inside default values instead of pattern also misses the point and bypasses Pydantic's internal optimization.
LIKELY FOLLOW-UPS: How would you reuse the SKU pattern across multiple models? What if the SKU format changes based on an environment variable? How do these constraints appear in the generated OpenAPI schema? What is the performance difference between pattern and a custom validator? Can you combine multiple constraints on a single Field?
ONE CONCRETE EXAMPLE: from pydantic import BaseModel, Field; class Item(BaseModel): price: float = Field(gt=0, description='Must be positive'); sku: str = Field(pattern=r'^ITEM-\d{5}$', description='SKU format'); item = Item(price=19.99, sku='ITEM-12345').
Source: pydantic.dev
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.