Files

39 lines
1023 B
Python

from __future__ import annotations
from datetime import datetime
from decimal import Decimal
from typing import Annotated
from pydantic import BaseModel, ConfigDict, Field, StrictBool, StrictInt, StrictStr, StringConstraints
NameField = Annotated[
StrictStr,
StringConstraints(strip_whitespace=True, min_length=3, max_length=120),
]
DescriptionField = Annotated[
StrictStr,
StringConstraints(strip_whitespace=True, min_length=1, max_length=1000),
]
class ProductCreate(BaseModel):
model_config = ConfigDict(extra="forbid")
name: NameField
description: DescriptionField | None = None
price: Annotated[Decimal, Field(gt=0, max_digits=10, decimal_places=2)]
stock: Annotated[StrictInt, Field(ge=0, le=1_000_000)]
is_active: StrictBool = True
class ProductResponse(BaseModel):
model_config = ConfigDict(from_attributes=True, extra="forbid")
id: int
name: str
description: str | None
price: Decimal
stock: int
is_active: bool
created_at: datetime