Files
mtusi2026lab2/src/models.py
T
2026-10-08 17:04:26 +03:00

117 lines
3.1 KiB
Python

from datetime import datetime
from uuid import UUID
from pydantic import BaseModel, ConfigDict, Field, field_validator
class CreateUserRequest(BaseModel):
model_config = ConfigDict(extra="forbid", strict=True)
username: str = Field(min_length=3, max_length=32, pattern=r"^[a-z0-9_]{3,32}$")
display_name: str = Field(min_length=1, max_length=100)
password: str = Field(min_length=12, max_length=128)
@field_validator("display_name")
@classmethod
def display_name_must_not_be_whitespace(cls, value: str) -> str:
"""Не допускает обязательное имя, состоящее только из пробелов."""
if not value.strip():
raise ValueError("display_name не может состоять только из пробелов")
return value
class CreateChatRequest(BaseModel):
model_config = ConfigDict(extra="forbid")
title: str = Field(min_length=1, max_length=100)
member_ids: list[UUID] = Field(min_length=1, max_length=100)
@field_validator("title")
@classmethod
def title_must_not_be_whitespace(cls, value: str) -> str:
"""Не допускает обязательное название, состоящее только из пробелов."""
if not value.strip():
raise ValueError("title не может состоять только из пробелов")
return value
class ChatPageResponse(BaseModel):
model_config = ConfigDict(extra="forbid")
items: list["Chat"]
next_cursor: str | None
class CreateMessageRequest(BaseModel):
model_config = ConfigDict(extra="forbid", strict=True)
text: str = Field(min_length=1, max_length=4000)
@field_validator("text")
@classmethod
def text_must_not_be_whitespace(cls, value: str) -> str:
"""Не допускает сообщение, состоящее только из пробельных символов."""
if not value.strip():
raise ValueError("text не может состоять только из пробелов")
return value
class MessagePageResponse(BaseModel):
model_config = ConfigDict(extra="forbid")
items: list["Message"]
next_cursor: str | None
class ErrorDetails(BaseModel):
model_config = ConfigDict(extra="forbid")
code: str
message: str
request_id: str
class ErrorResponse(BaseModel):
model_config = ConfigDict(extra="forbid")
error: ErrorDetails
class User(BaseModel):
model_config = ConfigDict(extra="forbid")
id: UUID
username: str
display_name: str
created_at: datetime
class Chat(BaseModel):
model_config = ConfigDict(extra="forbid")
id: UUID
title: str
member_ids: list[UUID]
created_at: datetime
class Message(BaseModel):
model_config = ConfigDict(extra="forbid")
id: UUID
chat_id: UUID
sender_id: UUID
text: str
created_at: datetime
class Session(BaseModel):
model_config = ConfigDict(extra="forbid")
token: str = Field(min_length=32)
token_type: str = "Bearer"
expires_at: datetime
user: User