forked from mtusi2026/mtusi2026lab6
121 lines
2.9 KiB
Python
121 lines
2.9 KiB
Python
"""Strict lab 5 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, model_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: str = Field(max_length=4000)
|
|
attachment_ids: list[Identifier] = Field(default_factory=list, max_length=10)
|
|
|
|
@model_validator(mode="after")
|
|
def valid_content(self):
|
|
if (
|
|
not self.text.strip()
|
|
and not self.attachment_ids
|
|
or any(0xD800 <= ord(c) <= 0xDFFF for c in self.text)
|
|
or len(set(self.attachment_ids)) != len(self.attachment_ids)
|
|
):
|
|
raise ValueError("Некорректное содержимое сообщения")
|
|
return self
|
|
|
|
|
|
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):
|
|
attachment_ids: list[str]
|
|
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
|
|
|
|
|
|
class AttachmentView(View):
|
|
id: str
|
|
chat_id: str
|
|
owner_id: str
|
|
filename: str
|
|
content_type: str
|
|
size_bytes: int
|
|
sha256: str
|
|
status: str
|
|
variants: list
|
|
error_code: str | None
|
|
created_at: str
|
|
updated_at: str
|