Files
mtusi2026lab2/app/models.py
T

30 lines
995 B
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)
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)
@field_validator("member_ids")
@classmethod
def _validate_members(cls, value: list[str]) -> list[str]:
seen: set[str] = set()
for raw in value:
UUID(raw) # бросит ValueError -> RequestValidationError -> 400
if raw in seen:
raise ValueError("duplicate member_ids")
seen.add(raw)
return value
class CreateMessage(BaseModel):
model_config = ConfigDict(extra="forbid")
text: str = Field(min_length=1, max_length=4000)