2025-05-24 15:14:55 +00:00

42 lines
954 B
Python

from datetime import datetime
from typing import Optional
from pydantic import BaseModel, Field, validator
# Shared properties
class TodoBase(BaseModel):
title: str
description: Optional[str] = None
completed: bool = False
# Properties to receive via API on creation
class TodoCreate(TodoBase):
title: str = Field(..., min_length=1, max_length=100)
@validator("title")
def title_must_not_be_empty(cls, v):
if not v.strip():
raise ValueError("Title must not be empty or whitespace only")
return v
# Properties to receive via API on update
class TodoUpdate(TodoBase):
title: Optional[str] = None
completed: Optional[bool] = None
# Properties shared by models stored in DB
class TodoInDBBase(TodoBase):
id: int
created_at: datetime
updated_at: datetime
class Config:
from_attributes = True
# Properties to return to client
class Todo(TodoInDBBase):
pass