70 lines
2.1 KiB
Python
70 lines
2.1 KiB
Python
"""API schemas (Pydantic) for Child.
|
|
|
|
Schemas validate request bodies and shape responses. Separate from domain entities
|
|
so the wire format can evolve independently.
|
|
"""
|
|
|
|
from datetime import date, datetime
|
|
from uuid import UUID
|
|
|
|
from pydantic import BaseModel, Field, field_validator
|
|
|
|
from app.entities.child import Child
|
|
|
|
|
|
class ChildCreate(BaseModel):
|
|
workspace_id: UUID
|
|
name: str = Field(min_length=1, max_length=255)
|
|
birth_date: date
|
|
preschool_start_date: date | None = None
|
|
|
|
@field_validator("preschool_start_date")
|
|
@classmethod
|
|
def validate_preschool_date(cls, v: date | None, info) -> date | None:
|
|
"""Preschool start date must be after birth date if both provided."""
|
|
if v is None:
|
|
return v
|
|
birth_date = info.data.get("birth_date")
|
|
if birth_date and v <= birth_date:
|
|
raise ValueError("preschool_start_date must be after birth_date")
|
|
return v
|
|
|
|
|
|
class ChildUpdate(BaseModel):
|
|
"""Partial update — all fields optional."""
|
|
|
|
name: str | None = Field(None, min_length=1, max_length=255)
|
|
birth_date: date | None = None
|
|
preschool_start_date: date | None = None
|
|
|
|
@field_validator("preschool_start_date")
|
|
@classmethod
|
|
def validate_preschool_date(cls, v: date | None, info) -> date | None:
|
|
"""Preschool start date must be after birth date if both provided."""
|
|
if v is None:
|
|
return v
|
|
birth_date = info.data.get("birth_date")
|
|
if birth_date and v <= birth_date:
|
|
raise ValueError("preschool_start_date must be after birth_date")
|
|
return v
|
|
|
|
|
|
class ChildResponse(BaseModel):
|
|
id: UUID
|
|
workspace_id: UUID
|
|
name: str
|
|
birth_date: date
|
|
preschool_start_date: date | None
|
|
created_at: datetime | None
|
|
|
|
@classmethod
|
|
def from_entity(cls, entity: Child) -> "ChildResponse":
|
|
return cls(
|
|
id=entity.id,
|
|
workspace_id=entity.workspace_id,
|
|
name=entity.name,
|
|
birth_date=entity.birth_date,
|
|
preschool_start_date=entity.preschool_start_date,
|
|
created_at=entity.created_at,
|
|
)
|