suhail
Initial deployment
7ffe51d
"""Pydantic schemas for Task CRUD operations."""
from datetime import datetime
from typing import Optional
from pydantic import BaseModel, Field, ConfigDict
class TaskCreate(BaseModel):
"""Schema for creating a new task."""
title: str = Field(..., min_length=1, max_length=200, description="Task title")
description: Optional[str] = Field(None, max_length=1000, description="Task description")
model_config = ConfigDict(
json_schema_extra={
"example": {
"title": "Buy groceries",
"description": "Milk, eggs, bread"
}
}
)
class TaskResponse(BaseModel):
"""Schema for task response."""
id: int = Field(..., description="Task ID")
user_id: int = Field(..., description="User ID who owns the task")
title: str = Field(..., description="Task title")
description: Optional[str] = Field(None, description="Task description")
completed: bool = Field(..., description="Task completion status")
created_at: datetime = Field(..., description="Task creation timestamp")
updated_at: datetime = Field(..., description="Task last update timestamp")
model_config = ConfigDict(
from_attributes=True,
json_schema_extra={
"example": {
"id": 1,
"user_id": 1,
"title": "Buy groceries",
"description": "Milk, eggs, bread",
"completed": False,
"created_at": "2026-01-08T10:00:00Z",
"updated_at": "2026-01-08T10:00:00Z"
}
}
)
class TaskUpdate(BaseModel):
"""Schema for updating a task (PUT - replaces all fields)."""
title: str = Field(..., min_length=1, max_length=200, description="Task title")
description: Optional[str] = Field(None, max_length=1000, description="Task description")
completed: bool = Field(..., description="Task completion status")
model_config = ConfigDict(
json_schema_extra={
"example": {
"title": "Buy groceries",
"description": "Milk, eggs, bread, cheese",
"completed": False
}
}
)
class TaskPatch(BaseModel):
"""Schema for partially updating a task (PATCH - updates only provided fields)."""
title: Optional[str] = Field(None, min_length=1, max_length=200, description="Task title")
description: Optional[str] = Field(None, max_length=1000, description="Task description")
completed: Optional[bool] = Field(None, description="Task completion status")
model_config = ConfigDict(
json_schema_extra={
"example": {
"completed": True
}
}
)
class TaskListResponse(BaseModel):
"""Schema for list of tasks response."""
tasks: list[TaskResponse] = Field(..., description="List of tasks")
total: int = Field(..., description="Total number of tasks")
model_config = ConfigDict(
json_schema_extra={
"example": {
"tasks": [
{
"id": 1,
"user_id": 1,
"title": "Buy groceries",
"description": "Milk, eggs, bread",
"completed": False,
"created_at": "2026-01-08T10:00:00Z",
"updated_at": "2026-01-08T10:00:00Z"
}
],
"total": 1
}
}
)