42 lines
1.6 KiB
Python
42 lines
1.6 KiB
Python
from pydantic import BaseModel, Field
|
|
from typing import Optional
|
|
from datetime import datetime
|
|
from uuid import UUID
|
|
|
|
# Base schema for Fruit, used for inheritance
|
|
class FruitBase(BaseModel):
|
|
name: str = Field(..., description="Name of the fruit")
|
|
description: Optional[str] = Field(None, description="Description of the fruit")
|
|
color: Optional[str] = Field(None, description="Color of the fruit")
|
|
is_seasonal: bool = Field(False, description="Whether the fruit is seasonal")
|
|
|
|
# Schema for creating a new Fruit
|
|
class FruitCreate(FruitBase):
|
|
pass
|
|
|
|
# Schema for updating an existing Fruit (all fields optional)
|
|
class FruitUpdate(BaseModel):
|
|
name: Optional[str] = Field(None, description="Name of the fruit")
|
|
description: Optional[str] = Field(None, description="Description of the fruit")
|
|
color: Optional[str] = Field(None, description="Color of the fruit")
|
|
is_seasonal: Optional[bool] = Field(None, description="Whether the fruit is seasonal")
|
|
|
|
# Schema for representing a Fruit in responses
|
|
class FruitSchema(FruitBase):
|
|
id: UUID
|
|
created_at: datetime
|
|
updated_at: datetime
|
|
|
|
class Config:
|
|
orm_mode = True
|
|
schema_extra = {
|
|
"example": {
|
|
"id": "f47ac10b-58cc-4372-a567-0e02b2c3d479",
|
|
"name": "Apple",
|
|
"description": "A round fruit with red, yellow, or green skin and crisp flesh",
|
|
"color": "Red",
|
|
"is_seasonal": True,
|
|
"created_at": "2023-01-01T12:00:00",
|
|
"updated_at": "2023-01-01T12:00:00"
|
|
}
|
|
} |