Files
mtusi2026lab3/app/models.py
T

88 lines
1.8 KiB
Python

from pydantic import BaseModel, Field, ConfigDict, field_validator
from typing import List, Optional
def _not_blank(value: str) -> str:
if not value.strip():
raise ValueError("must contain non-whitespace characters")
return value
class User(BaseModel):
id: str
username: str
display_name: str
created_at: str
class CreateUser(BaseModel):
model_config = ConfigDict(extra="forbid")
username: str = Field(..., 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)
_check_display_name = field_validator("display_name")(_not_blank)
class Chat(BaseModel):
id: str
title: str
member_ids: List[str]
created_at: str
class CreateChat(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)
_check_title = field_validator("title")(_not_blank)
class Message(BaseModel):
id: str
chat_id: str
sender_id: str
text: str
created_at: str
class CreateMessage(BaseModel):
model_config = ConfigDict(extra="forbid")
text: str = Field(..., min_length=1, max_length=4000)
_check_text = field_validator("text")(_not_blank)
class ChatPage(BaseModel):
items: List[Chat]
next_cursor: Optional[str] = None
class MessagePage(BaseModel):
items: List[Message]
next_cursor: Optional[str] = None
class Health(BaseModel):
status: str = "ok"
class Session(BaseModel):
token: str
token_type: str = "Bearer"
expires_at: str
user: User
class ErrorBody(BaseModel):
code: str
message: str
request_id: str
class ErrorResponse(BaseModel):
error: ErrorBody