kanban-app/backend/app/schemas/product.py

72 lines
2.2 KiB
Python
Raw Normal View History

2026-02-24 14:17:36 +00:00
"""Pydantic schemas for Product model"""
from datetime import datetime
2026-02-24 16:19:15 +00:00
from decimal import Decimal
2026-02-24 14:17:36 +00:00
from typing import Optional
2026-02-24 16:19:15 +00:00
from pydantic import BaseModel, ConfigDict, Field, field_validator
2026-02-24 14:17:36 +00:00
class ProductCreateRequest(BaseModel):
"""Schema for creating a new product"""
2026-02-24 16:19:15 +00:00
2026-02-24 14:36:31 +00:00
model_config = ConfigDict(
json_schema_extra={
2026-02-24 14:17:36 +00:00
"example": {
"name": "Handcrafted Wooden Bowl",
"description": "A beautiful handcrafted bowl made from oak",
"price": 45.99,
"stock": 10,
2026-02-24 16:19:15 +00:00
"image_url": "https://example.com/bowl.jpg",
2026-02-24 14:17:36 +00:00
}
}
2026-02-24 14:36:31 +00:00
)
2026-02-24 16:19:15 +00:00
2026-02-24 14:36:31 +00:00
name: str = Field(..., min_length=1, max_length=200, description="Product name")
description: Optional[str] = Field(None, description="Product description")
2026-02-24 16:19:15 +00:00
price: Decimal = Field(
..., gt=0, description="Product price (must be greater than 0)"
)
2026-02-24 14:36:31 +00:00
stock: int = Field(default=0, ge=0, description="Product stock quantity")
2026-02-24 16:19:15 +00:00
image_url: Optional[str] = Field(
None, max_length=500, description="Product image URL"
)
2026-02-24 14:17:36 +00:00
@field_validator("price")
@classmethod
def validate_price(cls, v: Decimal) -> Decimal:
"""Validate that price has at most 2 decimal places"""
if v.as_tuple().exponent < -2:
raise ValueError("Price must have at most 2 decimal places")
return v
class ProductResponse(BaseModel):
"""Schema for product response"""
2026-02-24 16:19:15 +00:00
2026-02-24 14:36:31 +00:00
model_config = ConfigDict(
from_attributes=True,
json_schema_extra={
2026-02-24 14:17:36 +00:00
"example": {
"id": 1,
"name": "Handcrafted Wooden Bowl",
"description": "A beautiful handcrafted bowl made from oak",
"price": 45.99,
"stock": 10,
"image_url": "https://example.com/bowl.jpg",
"is_active": True,
"created_at": "2024-01-15T10:30:00",
2026-02-24 16:19:15 +00:00
"updated_at": "2024-01-15T10:30:00",
2026-02-24 14:17:36 +00:00
}
2026-02-24 16:19:15 +00:00
},
2026-02-24 14:36:31 +00:00
)
2026-02-24 16:19:15 +00:00
2026-02-24 14:36:31 +00:00
id: int
name: str
description: Optional[str] = None
price: float
stock: int
image_url: Optional[str] = None
is_active: bool
created_at: Optional[datetime] = None
updated_at: Optional[datetime] = None