Files
mtusi2026lab4/src/models.py
T

136 lines
3.4 KiB
Python

from typing import Optional, List
from pydantic import BaseModel, Field, field_validator, ConfigDict
import re
class CreateUserRequest(BaseModel):
model_config = ConfigDict(extra="forbid")
username: str = Field(..., min_length=3, max_length=32)
display_name: str = Field(..., min_length=1, max_length=100)
password: str = Field(..., min_length=12, max_length=128)
@field_validator('username')
@classmethod
def validate_username(cls, v):
if not re.match(r'^[a-z0-9_]{3,32}$', v):
raise ValueError('username должен содержать только a-z, 0-9, _ (3-32 символа)')
return v
@field_validator('display_name')
@classmethod
def validate_display_name(cls, v):
if not v.strip():
raise ValueError('display_name не может состоять только из пробелов')
return v
@field_validator('password')
@classmethod
def validate_password(cls, v):
if not v.strip():
raise ValueError('password не может состоять только из пробелов')
return v
class CreateChatRequest(BaseModel):
model_config = ConfigDict(extra="forbid")
title: str = Field(..., min_length=1, max_length=100)
member_ids: List[str] = Field(..., min_length=1, max_length=100)
@field_validator('title')
@classmethod
def validate_title(cls, v):
if not v.strip():
raise ValueError('title не может состоять только из пробелов')
return v
class CreateMessageRequest(BaseModel):
model_config = ConfigDict(extra="forbid")
text: str = Field(..., max_length=4000)
attachment_ids: List[str] = Field(default_factory=list, max_length=10)
@field_validator('text')
@classmethod
def validate_text(cls, v):
if not v.strip() and not v == '':
raise ValueError('text не может состоять только из пробелов')
return v
@field_validator('attachment_ids')
@classmethod
def validate_attachment_ids(cls, v):
if len(v) != len(set(v)):
raise ValueError('attachment_ids должны быть уникальными')
return v
class UserResponse(BaseModel):
id: str
username: str
display_name: str
created_at: str
class ChatResponse(BaseModel):
id: str
title: str
member_ids: List[str]
created_at: str
class MessageResponse(BaseModel):
id: str
chat_id: str
sender_id: str
text: str
created_at: str
attachment_ids: List[str] = Field(default_factory=list)
class ChatPage(BaseModel):
items: List[ChatResponse]
next_cursor: Optional[str] = None
class MessagePage(BaseModel):
items: List[MessageResponse]
next_cursor: Optional[str] = None
class SessionResponse(BaseModel):
token: str
token_type: str = "Bearer"
expires_at: str
user: UserResponse
class HealthResponse(BaseModel):
status: str = "ok"
class AttachmentVariant(BaseModel):
name: str
content_type: str
size_bytes: int
width: int
height: int
sha256: str
class AttachmentResponse(BaseModel):
id: str
chat_id: str
owner_id: str
filename: str
content_type: str
size_bytes: int
sha256: str
status: str
variants: List[AttachmentVariant]
error_code: Optional[str]
created_at: str
updated_at: str