"""Schemas for the Author model.""" from pydantic import BaseModel, ConfigDict, Field, field_validator class AuthorBase(BaseModel): """Fields shared by create/update/read author schemas.""" name: str = Field(..., min_length=1, max_length=100) email: str = Field(..., min_length=3, max_length=255) bio: str | None = None @field_validator("email") @classmethod def _valid_email(cls, value: str) -> str: if "@" not in value or value.startswith("@") or value.endswith("@"): raise ValueError("must be a valid email address") return value class AuthorCreate(AuthorBase): """Payload for creating an author.""" class AuthorUpdate(BaseModel): """Payload for updating an author (all fields optional).""" name: str | None = Field(default=None, min_length=1, max_length=100) email: str | None = Field(default=None, min_length=3, max_length=255) bio: str | None = None class AuthorRead(AuthorBase): """Author as returned by the API.""" model_config = ConfigDict(from_attributes=True) id: int