forked from mtusi2026/mtusi2026lab2
141 lines
3.0 KiB
Python
141 lines
3.0 KiB
Python
from pydantic import BaseModel, ConfigDict, Field, field_validator
|
|
from uuid import UUID
|
|
|
|
|
|
class CreateUser(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,
|
|
)
|
|
|
|
@field_validator("display_name")
|
|
@classmethod
|
|
def display_name_must_not_be_blank(cls, value: str) -> str:
|
|
if not value.strip() or "\x00" in value:
|
|
raise ValueError("display_name must not be blank")
|
|
|
|
return value
|
|
|
|
|
|
password: str = Field(min_length=12, max_length=128)
|
|
|
|
@field_validator("password")
|
|
@classmethod
|
|
def password_must_not_be_blank(cls, value: str) -> str:
|
|
if not value.strip():
|
|
raise ValueError("password must not be blank")
|
|
return value
|
|
|
|
|
|
class User(BaseModel):
|
|
model_config = ConfigDict(extra="forbid")
|
|
|
|
id: str
|
|
username: str
|
|
display_name: str
|
|
created_at: str
|
|
|
|
|
|
class CreateChat(BaseModel):
|
|
model_config = ConfigDict(
|
|
extra="forbid",
|
|
strict=True,
|
|
)
|
|
|
|
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 title_must_not_be_blank(cls, value: str) -> str:
|
|
if not value.strip() or "\x00" in value:
|
|
raise ValueError("title must not be blank")
|
|
|
|
return value
|
|
|
|
@field_validator("member_ids")
|
|
@classmethod
|
|
def validate_member_ids(cls, value: list[str]) -> list[str]:
|
|
normalized = []
|
|
for member_id in value:
|
|
try:
|
|
normalized.append(str(UUID(member_id)))
|
|
except (ValueError, TypeError):
|
|
raise ValueError("member_ids must contain valid UUIDs") from None
|
|
|
|
if len(normalized) != len(set(normalized)):
|
|
raise ValueError("member_ids must be unique")
|
|
|
|
return normalized
|
|
|
|
|
|
class Chat(BaseModel):
|
|
model_config = ConfigDict(extra="forbid")
|
|
|
|
id: str
|
|
title: str
|
|
member_ids: list[str]
|
|
created_at: str
|
|
|
|
|
|
class ChatPage(BaseModel):
|
|
model_config = ConfigDict(extra="forbid")
|
|
|
|
items: list[Chat]
|
|
next_cursor: str | None
|
|
|
|
|
|
class CreateMessage(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_blank(cls, value: str) -> str:
|
|
if not value.strip() or "\x00" in value:
|
|
raise ValueError("text must not be blank")
|
|
|
|
return value
|
|
|
|
|
|
class Message(BaseModel):
|
|
model_config = ConfigDict(extra="forbid")
|
|
|
|
id: str
|
|
chat_id: str
|
|
sender_id: str
|
|
text: str
|
|
created_at: str
|
|
|
|
|
|
class MessagePage(BaseModel):
|
|
model_config = ConfigDict(extra="forbid")
|
|
|
|
items: list[Message]
|
|
next_cursor: str | None
|