forked from mtusi2026/mtusi2026lab2
118 lines
2.4 KiB
Python
118 lines
2.4 KiB
Python
from datetime import datetime
|
|
from uuid import UUID
|
|
|
|
from pydantic import BaseModel, ConfigDict, Field, field_validator
|
|
|
|
|
|
class CreateUser(BaseModel):
|
|
model_config = ConfigDict(extra="forbid")
|
|
|
|
username: str = Field(
|
|
min_length=3,
|
|
max_length=32,
|
|
pattern=r"^[a-z0-9_]+$",
|
|
)
|
|
|
|
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 validate_display_name(cls, value: str) -> str:
|
|
if not value.strip():
|
|
raise ValueError("Display name must not be blank")
|
|
|
|
return value
|
|
|
|
|
|
class User(BaseModel):
|
|
id: UUID
|
|
username: str
|
|
display_name: str
|
|
created_at: datetime
|
|
|
|
|
|
class SessionResponse(BaseModel):
|
|
token: str
|
|
token_type: str
|
|
expires_at: datetime
|
|
user: User
|
|
|
|
|
|
class CreateChat(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 validate_title(cls, value: str) -> str:
|
|
if not value.strip():
|
|
raise ValueError("Chat title must not be blank")
|
|
|
|
return value
|
|
|
|
@field_validator("member_ids")
|
|
@classmethod
|
|
def validate_member_ids(cls, value: list[UUID]) -> list[UUID]:
|
|
if len(value) != len(set(value)):
|
|
raise ValueError("Member IDs must be unique")
|
|
|
|
return value
|
|
|
|
|
|
class Chat(BaseModel):
|
|
id: UUID
|
|
title: str
|
|
member_ids: list[UUID]
|
|
created_at: datetime
|
|
|
|
|
|
class ChatPage(BaseModel):
|
|
items: list[Chat]
|
|
next_cursor: str | None
|
|
|
|
|
|
class CreateMessage(BaseModel):
|
|
model_config = ConfigDict(extra="forbid")
|
|
|
|
text: str = Field(
|
|
min_length=1,
|
|
max_length=4000,
|
|
)
|
|
|
|
@field_validator("text")
|
|
@classmethod
|
|
def validate_text(cls, value: str) -> str:
|
|
if not value.strip():
|
|
raise ValueError("Message text must not be blank")
|
|
|
|
return value
|
|
|
|
|
|
class Message(BaseModel):
|
|
id: UUID
|
|
chat_id: UUID
|
|
sender_id: UUID
|
|
text: str
|
|
created_at: datetime
|
|
|
|
|
|
class MessagePage(BaseModel):
|
|
items: list[Message]
|
|
next_cursor: str | None |