forked from mtusi2026/mtusi2026lab4
Добавил загрузку картинок в S3 и проверку доступа к вложениям
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
"""Strict request models; strings retain their original Unicode content."""
|
||||
import re
|
||||
from uuid import UUID
|
||||
|
||||
from pydantic import BaseModel, ConfigDict, Field, field_validator, model_validator
|
||||
|
||||
|
||||
class Input(BaseModel):
|
||||
model_config = ConfigDict(strict=True, extra="forbid")
|
||||
|
||||
@field_validator("*", check_fields=False)
|
||||
@classmethod
|
||||
def valid_text(cls, value):
|
||||
if isinstance(value, str):
|
||||
if not value.strip():
|
||||
raise ValueError("A string must not be blank")
|
||||
value.encode("utf-8")
|
||||
if "\x00" in value:
|
||||
raise ValueError("NUL is not supported")
|
||||
return value
|
||||
|
||||
|
||||
class CreateUser(Input):
|
||||
username: str
|
||||
display_name: str = Field(min_length=1, max_length=100)
|
||||
password: str = Field(min_length=12, max_length=128)
|
||||
|
||||
@field_validator("username")
|
||||
@classmethod
|
||||
def username_format(cls, value):
|
||||
if re.fullmatch(r"[a-z0-9_]{3,32}", value) is None:
|
||||
raise ValueError("Invalid username")
|
||||
return value
|
||||
|
||||
|
||||
class CreateChat(Input):
|
||||
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 members(cls, values):
|
||||
normalized = [str(UUID(value)) for value in values]
|
||||
if len(set(normalized)) != len(normalized):
|
||||
raise ValueError("Duplicate members")
|
||||
return normalized
|
||||
|
||||
|
||||
class CreateMessage(Input):
|
||||
text: str = Field(max_length=4000)
|
||||
attachment_ids: list[str] = Field(default_factory=list, max_length=10)
|
||||
|
||||
@field_validator("text")
|
||||
@classmethod
|
||||
def valid_text(cls, value):
|
||||
value.encode("utf-8")
|
||||
if "\x00" in value:
|
||||
raise ValueError("Invalid text")
|
||||
return value
|
||||
|
||||
@field_validator("attachment_ids")
|
||||
@classmethod
|
||||
def attachments(cls, values):
|
||||
normalized = [str(UUID(value)) for value in values]
|
||||
if len(set(normalized)) != len(normalized):
|
||||
raise ValueError("Duplicate attachments")
|
||||
return normalized
|
||||
|
||||
@model_validator(mode="after")
|
||||
def not_empty(self):
|
||||
if not self.text.strip() and not self.attachment_ids:
|
||||
raise ValueError("Message is empty")
|
||||
return self
|
||||
Reference in New Issue
Block a user