import re from uuid import UUID from pydantic import BaseModel, ConfigDict, Field, StrictStr, field_validator class RequestModel(BaseModel): model_config = ConfigDict(extra="forbid") @field_validator("*", mode="after") @classmethod def nonblank(cls, value): if isinstance(value, str) and not value.strip(): raise ValueError("Строка не может состоять из пробелов") return value class CreateUser(RequestModel): username: StrictStr = Field(pattern=r"^[a-z0-9_]{3,32}$") display_name: StrictStr = Field(min_length=1, max_length=100) password: StrictStr = Field(min_length=12, max_length=128) @field_validator("username") @classmethod def full_username(cls, value): if not re.fullmatch(r"[a-z0-9_]{3,32}", value): raise ValueError("Неверное имя пользователя") return value class CreateChat(RequestModel): title: StrictStr = Field(min_length=1, max_length=100) member_ids: list[UUID] = Field(min_length=1, max_length=100) @field_validator("member_ids", mode="before") @classmethod def string_ids(cls, value): if not isinstance(value, list) or any(not isinstance(x, str) for x in value): raise ValueError("Ожидается массив UUID-строк") return value @field_validator("member_ids") @classmethod def unique_ids(cls, value): if len(set(value)) != len(value): raise ValueError("Участники не должны повторяться") return value class CreateMessage(RequestModel): text: StrictStr = Field(min_length=1, max_length=4000)