forked from mtusi2026/mtusi2026lab5
prepared skeleton for lab5
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
from datetime import datetime
|
||||
from typing import Self
|
||||
from uuid import UUID
|
||||
|
||||
from pydantic import Field, field_validator, model_validator
|
||||
|
||||
from app.schemas.base import Base, BaseCreate, BasePage
|
||||
|
||||
|
||||
class Message(Base):
|
||||
chat_id: UUID
|
||||
sender_id: UUID
|
||||
text: str = Field(max_length=4000)
|
||||
attachment_ids: list[UUID] = Field(default_factory=list, max_length=10)
|
||||
created_at: datetime
|
||||
|
||||
|
||||
class CreateMessage(BaseCreate):
|
||||
text: str = Field(max_length=4000)
|
||||
attachment_ids: list[UUID] = Field(default_factory=list, max_length=10)
|
||||
|
||||
@field_validator("*", mode="before")
|
||||
@classmethod
|
||||
def reject_invalid_strings(cls, value: object) -> object:
|
||||
if isinstance(value, str):
|
||||
if "\x00" in value or (value != "" and not value.strip()):
|
||||
raise ValueError("Некорректная строка сообщения")
|
||||
return value
|
||||
|
||||
@model_validator(mode="after")
|
||||
def require_content(self) -> Self:
|
||||
if not self.text and not self.attachment_ids:
|
||||
raise ValueError("Нужен текст или вложение")
|
||||
if len(self.attachment_ids) != len(set(self.attachment_ids)):
|
||||
raise ValueError("attachment_ids содержит дубликаты")
|
||||
return self
|
||||
|
||||
|
||||
class MessagePage(BasePage):
|
||||
items: list[Message]
|
||||
next_cursor: str | None
|
||||
Reference in New Issue
Block a user