Constrain fields with Field
A valid Python type can still carry an invalid domain value. A negative quantity is an integer, and an empty label is a string. Field constraints combine those domain rules with the type so every construction path checks the same conditions.
Use inclusive and exclusive bounds carefully: ge includes its boundary, while gt excludes it. String length is checked on the value you actually validate. If whitespace should be removed first, add an explicit normalization rule rather than assuming length validation strips it.
from pydantic import BaseModel, Field
class Page(BaseModel):
size: int = Field(ge=1, le=50)
print(Page(size=10).model_dump())Your task
Define Product with name length 1..40, price greater than zero, and stock integer at least zero. product_data(raw) returns its validated dictionary.
The explanation opens here once every check passes.