forked from mtusi2026/mtusi2026lab5
40 lines
996 B
Python
40 lines
996 B
Python
from typing import List, Optional
|
|
|
|
from pydantic import Field, StrictStr, field_validator, model_validator
|
|
from uuid import UUID
|
|
|
|
from .common import ContractModel
|
|
|
|
|
|
|
|
class CreateMessage(ContractModel):
|
|
text: StrictStr = Field(max_length=4000)
|
|
attachment_ids: List[StrictStr] = Field(default_factory=list, max_length=10)
|
|
|
|
@field_validator('attachment_ids')
|
|
def validate_ids(cls, value):
|
|
ids = [str(UUID(item)) for item in value]
|
|
if len(ids) != len(set(ids)):
|
|
raise ValueError('duplicate attachments')
|
|
return ids
|
|
|
|
@model_validator(mode='after')
|
|
def validate_content(self):
|
|
if not self.text.strip() and not self.attachment_ids:
|
|
raise ValueError('empty message')
|
|
return self
|
|
|
|
|
|
class Message(ContractModel):
|
|
id: str
|
|
chat_id: str
|
|
sender_id: str
|
|
text: str
|
|
attachment_ids: List[str]
|
|
created_at: str
|
|
|
|
|
|
class MessagePage(ContractModel):
|
|
items: List[Message]
|
|
next_cursor: Optional[str]
|