34 lines
772 B
Python
34 lines
772 B
Python
from typing import Optional
|
|
|
|
from pydantic import BaseModel, Field
|
|
|
|
|
|
# Base Product Schema
|
|
class ProductBase(BaseModel):
|
|
name: str
|
|
description: Optional[str] = None
|
|
price: float = Field(..., gt=0)
|
|
stock: int = Field(..., ge=0)
|
|
image_url: Optional[str] = None
|
|
|
|
|
|
# Schema for creating a new product
|
|
class ProductCreate(ProductBase):
|
|
pass
|
|
|
|
|
|
# Schema for updating an existing product
|
|
class ProductUpdate(BaseModel):
|
|
name: Optional[str] = None
|
|
description: Optional[str] = None
|
|
price: Optional[float] = Field(None, gt=0)
|
|
stock: Optional[int] = Field(None, ge=0)
|
|
image_url: Optional[str] = None
|
|
|
|
|
|
# Schema for returning product data
|
|
class ProductResponse(ProductBase):
|
|
id: int
|
|
|
|
class Config:
|
|
from_attributes = True |