forked from mtusi2026/mtusi2026lab4
реализация 4 лабы
This commit is contained in:
+19
-4
@@ -2,7 +2,7 @@
|
||||
|
||||
from uuid import UUID
|
||||
|
||||
from pydantic import BaseModel, ConfigDict, Field, field_validator
|
||||
from pydantic import BaseModel, ConfigDict, Field, ValidationInfo, field_validator, model_validator
|
||||
|
||||
|
||||
class RequestModel(BaseModel):
|
||||
@@ -10,9 +10,9 @@ class RequestModel(BaseModel):
|
||||
|
||||
@field_validator("*")
|
||||
@classmethod
|
||||
def check_strings(cls, value):
|
||||
def check_strings(cls, value, info: ValidationInfo):
|
||||
if isinstance(value, str):
|
||||
if not value.strip():
|
||||
if info.field_name != "text" and not value.strip():
|
||||
raise ValueError("Строка не должна состоять из пробелов")
|
||||
value.encode("utf-8")
|
||||
return value
|
||||
@@ -38,4 +38,19 @@ class CreateChat(RequestModel):
|
||||
|
||||
|
||||
class CreateMessage(RequestModel):
|
||||
text: str = Field(min_length=1, max_length=4000)
|
||||
text: str = Field(max_length=4000)
|
||||
attachment_ids: list[str] = Field(default_factory=list, max_length=10)
|
||||
|
||||
@field_validator("attachment_ids")
|
||||
@classmethod
|
||||
def check_attachments(cls, values):
|
||||
ids = [str(UUID(value)) for value in values]
|
||||
if len(ids) != len(set(ids)):
|
||||
raise ValueError("Вложения не должны повторяться")
|
||||
return ids
|
||||
|
||||
@model_validator(mode="after")
|
||||
def check_content(self):
|
||||
if not self.text.strip() and not self.attachment_ids:
|
||||
raise ValueError("Нужен текст или хотя бы одно вложение")
|
||||
return self
|
||||
|
||||
Reference in New Issue
Block a user