"""допустимые поля и проверка данных""" from uuid import UUID from pydantic import BaseModel, ConfigDict, Field, ValidationInfo, field_validator, model_validator class RequestModel(BaseModel): model_config = ConfigDict(extra="forbid", strict=True) @field_validator("*") @classmethod def check_strings(cls, value, info: ValidationInfo): if isinstance(value, str): if info.field_name != "text" and not value.strip(): raise ValueError("Строка не должна состоять из пробелов") value.encode("utf-8") return value class CreateUser(RequestModel): username: str = Field(pattern=r"^[a-z0-9_]{3,32}$") display_name: str = Field(min_length=1, max_length=100) password: str = Field(min_length=12, max_length=128) class CreateChat(RequestModel): title: str = Field(min_length=1, max_length=100) member_ids: list[str] = Field(min_length=1, max_length=100) @field_validator("member_ids") @classmethod def check_members(cls, values): member_ids = [str(UUID(value)) for value in values] if len(set(member_ids)) != len(member_ids): raise ValueError("Участники не должны повторяться") return member_ids class CreateMessage(RequestModel): 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