feat: add private attachments and safe reconciliation experiment
This commit is contained in:
@@ -0,0 +1,68 @@
|
||||
import re
|
||||
from uuid import UUID
|
||||
|
||||
from pydantic import BaseModel, ConfigDict, Field, StrictStr, field_validator, model_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(BaseModel):
|
||||
model_config = ConfigDict(extra="forbid")
|
||||
text: StrictStr = Field(max_length=4000)
|
||||
attachment_ids: list[UUID] = Field(default_factory=list, max_length=10)
|
||||
|
||||
@field_validator("attachment_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
|
||||
|
||||
@model_validator(mode="after")
|
||||
def content(self):
|
||||
if len(set(self.attachment_ids)) != len(self.attachment_ids):
|
||||
raise ValueError("Вложения не должны повторяться")
|
||||
if not self.text.strip() and not self.attachment_ids:
|
||||
raise ValueError("Пустое сообщение без вложений")
|
||||
return self
|
||||
Reference in New Issue
Block a user