external_link.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. from datetime import datetime
  2. from pydantic import BaseModel, Field, field_validator
  3. class ExternalLinkBase(BaseModel):
  4. """Base schema for external links."""
  5. name: str = Field(..., min_length=1, max_length=50, description="Display name for the link")
  6. url: str = Field(..., min_length=1, max_length=500, description="External URL")
  7. icon: str = Field(default="link", max_length=50, description="Lucide icon name")
  8. open_in_new_tab: bool = False
  9. @field_validator("url")
  10. @classmethod
  11. def validate_url(cls, v: str) -> str:
  12. """Validate URL format."""
  13. if not v.startswith(("http://", "https://")):
  14. raise ValueError("URL must start with http:// or https://")
  15. return v
  16. class ExternalLinkCreate(ExternalLinkBase):
  17. """Schema for creating an external link."""
  18. pass
  19. class ExternalLinkUpdate(BaseModel):
  20. """Schema for updating an external link (all fields optional)."""
  21. name: str | None = Field(default=None, min_length=1, max_length=50)
  22. url: str | None = Field(default=None, min_length=1, max_length=500)
  23. icon: str | None = Field(default=None, max_length=50)
  24. open_in_new_tab: bool | None = None
  25. @field_validator("url")
  26. @classmethod
  27. def validate_url(cls, v: str | None) -> str | None:
  28. """Validate URL format."""
  29. if v is not None and not v.startswith(("http://", "https://")):
  30. raise ValueError("URL must start with http:// or https://")
  31. return v
  32. class ExternalLinkResponse(ExternalLinkBase):
  33. """Response schema for external links."""
  34. id: int
  35. open_in_new_tab: bool
  36. custom_icon: str | None = None
  37. sort_order: int
  38. created_at: datetime
  39. updated_at: datetime
  40. model_config = {"from_attributes": True}
  41. class ExternalLinkReorder(BaseModel):
  42. """Schema for reordering external links."""
  43. ids: list[int] = Field(..., description="List of link IDs in desired order")