"""Strict lab 2 wire types; password never belongs to a response User.""" import re from typing import Annotated from uuid import UUID from pydantic import AfterValidator, BaseModel, ConfigDict, Field, field_validator def canonical_uuid(value: str) -> str: if not re.fullmatch(r"[0-9a-fA-F]{8}(?:-[0-9a-fA-F]{4}){3}-[0-9a-fA-F]{12}", value): raise ValueError("Ожидается UUID") return str(UUID(value)) def nonblank(value: str) -> str: if not value.strip() or any(0xD800 <= ord(char) <= 0xDFFF for char in value): raise ValueError("Ожидается непустая строка Unicode") return value type Identifier = Annotated[str, AfterValidator(canonical_uuid)] type ShortText = Annotated[str, Field(min_length=1, max_length=100), AfterValidator(nonblank)] class Input(BaseModel): model_config = ConfigDict(strict=True, extra="forbid") class CreateUser(Input): username: str = Field(min_length=3, max_length=32) display_name: ShortText password: Annotated[str, Field(min_length=12, max_length=128), AfterValidator(nonblank)] @field_validator("username") @classmethod def valid_username(cls, value: str) -> str: if not re.fullmatch(r"[a-z0-9_]{3,32}", value): raise ValueError("Недопустимое имя пользователя") return value class CreateChat(Input): title: ShortText member_ids: list[Identifier] = Field(min_length=1, max_length=100) class CreateMessage(Input): text: Annotated[str, Field(min_length=1, max_length=4000), AfterValidator(nonblank)] class View(BaseModel): model_config = ConfigDict(from_attributes=True, extra="forbid") class UserView(View): id: str username: str display_name: str created_at: str class SessionView(View): token: str token_type: str expires_at: str user: UserView class ChatView(View): id: str title: str member_ids: list[str] created_at: str class MessageView(View): id: str chat_id: str sender_id: str text: str created_at: str class ChatPage(View): items: list[ChatView] next_cursor: str | None class MessagePage(View): items: list[MessageView] next_cursor: str | None