27 lines
1.1 KiB
Python
27 lines
1.1 KiB
Python
from datetime import datetime
|
|
from typing import Optional
|
|
from pydantic import BaseModel, Field, ConfigDict
|
|
|
|
# Base Todo schema with common attributes
|
|
class TodoBase(BaseModel):
|
|
title: str = Field(..., min_length=1, max_length=100, description="Title of the todo item")
|
|
description: Optional[str] = Field(None, max_length=500, description="Optional description of the todo")
|
|
completed: bool = Field(False, description="Whether the todo is completed")
|
|
|
|
# Schema for creating a new Todo
|
|
class TodoCreate(TodoBase):
|
|
pass
|
|
|
|
# Schema for updating an existing Todo
|
|
class TodoUpdate(BaseModel):
|
|
title: Optional[str] = Field(None, min_length=1, max_length=100, description="Title of the todo item")
|
|
description: Optional[str] = Field(None, max_length=500, description="Optional description of the todo")
|
|
completed: Optional[bool] = Field(None, description="Whether the todo is completed")
|
|
|
|
# Schema for returning a Todo from the API
|
|
class Todo(TodoBase):
|
|
id: int
|
|
created_at: datetime
|
|
updated_at: Optional[datetime] = None
|
|
|
|
model_config = ConfigDict(from_attributes=True) # Updated configuration for Pydantic v2 |