forked from mtusi2026/mtusi2026lab2
56 lines
1.5 KiB
Python
56 lines
1.5 KiB
Python
from uuid import UUID
|
|
|
|
from pydantic import BaseModel, ConfigDict, Field, field_validator
|
|
|
|
|
|
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)
|
|
|
|
@field_validator("display_name", "password")
|
|
@classmethod
|
|
def _not_blank(cls, v: str) -> str:
|
|
if not v.strip():
|
|
raise ValueError("must not be blank")
|
|
return v
|
|
|
|
|
|
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 _title_not_blank(cls, v: str) -> str:
|
|
if not v.strip():
|
|
raise ValueError("title must not be blank")
|
|
return v
|
|
|
|
@field_validator("member_ids")
|
|
@classmethod
|
|
def _unique_members(cls, v: list[UUID]) -> list[UUID]:
|
|
if len(set(v)) != len(v):
|
|
raise ValueError("duplicate member_ids")
|
|
return v
|
|
|
|
|
|
class CreateMessage(BaseModel):
|
|
model_config = ConfigDict(extra="forbid")
|
|
text: str = Field(min_length=1, max_length=4000)
|
|
|
|
@field_validator("text")
|
|
@classmethod
|
|
def _text_not_blank(cls, v: str) -> str:
|
|
if not v.strip():
|
|
raise ValueError("text must not be blank")
|
|
return v
|
|
|
|
|
|
class SessionResponse(BaseModel):
|
|
token: str
|
|
token_type: str = "Bearer"
|
|
expires_at: str
|
|
user: dict |