Coverage for app / schemas.py: 100%

40 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-04-23 08:14 +0000

1from pydantic import BaseModel, Field, field_validator 

2from typing import Optional 

3from datetime import datetime 

4 

5VALID_PRIORITIES = ["low", "medium", "high"] 

6 

7class TaskCreate(BaseModel): 

8 title: str = Field(..., min_length=1, max_length=100) 

9 description: Optional[str] = Field(default="", max_length=500) 

10 priority: Optional[str] = Field(default="medium") 

11 

12 @field_validator("title") 

13 @classmethod 

14 def title_must_not_be_blank(cls, v): 

15 if not v.strip(): 

16 raise ValueError("Title cannot be blank") 

17 return v.strip() 

18 

19 @field_validator("priority") 

20 @classmethod 

21 def priority_must_be_valid(cls, v): 

22 if v not in VALID_PRIORITIES: 

23 raise ValueError(f"Priority must be one of: {VALID_PRIORITIES}") 

24 return v 

25 

26class TaskUpdate(BaseModel): 

27 title: Optional[str] = Field(default=None, min_length=1, max_length=100) 

28 description: Optional[str] = Field(default=None, max_length=500) 

29 priority: Optional[str] = None 

30 completed: Optional[bool] = None 

31 

32 @field_validator("priority") 

33 @classmethod 

34 def priority_must_be_valid(cls, v): 

35 if v is not None and v not in VALID_PRIORITIES: 

36 raise ValueError(f"Priority must be one of: {VALID_PRIORITIES}") 

37 return v 

38 

39class TaskResponse(BaseModel): 

40 id: int 

41 title: str 

42 description: str 

43 priority: str 

44 completed: bool 

45 created_at: Optional[datetime] = None 

46 updated_at: Optional[datetime] = None 

47 model_config = {"from_attributes": True}