Skip to content
tezvyn:

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

Source: pydantic.devMediumHow cards are made

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

Pydantic V2 Field constraints vs custom validators.

Key points

Use Field(gt=0) for price and Field(pattern=r'^ITEM-\d{5}$') for SKU; mention Annotated.

Watch out for

Suggesting @field_validator or conint/constr.

What's really being asked

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.

The full answer

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.

The mistakes people make

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.

What usually comes next

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?

A 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').

Interview question

In Pydantic V2, how should you enforce a positive price and a regex-formatted SKU without writing custom validators?

  • a.Supply the constraints through json_schema_extra so they apply at runtime validation
  • b.Set price: float = Field(gt=0) and sku: str = Field(pattern=r'^ITEM-\d{5}$') on standard typesCorrect
  • c.Use conint(gt=0) for price and constr(pattern=r'^ITEM-\d{5}$') for SKU in the model definition
  • d.Add @field_validator methods for price and sku to check values during model validation
Why?

Field's built-in gt and pattern parameters enforce constraints natively without extra code, while @field_validator adds unnecessary boilerplate and ignores Pydantic V2's native capabilities.

Just read this? Test yourself on what you have been reading.

Read the original → pydantic.dev

You just looked this up. Could you explain it out loud?

That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on python — each one lists the topics its interview covers.

See open roles