feat: establish lab1 HTTP contract skeleton

This commit is contained in:
Самохвалов Андрей Ремович
2026-10-01 21:17:35 +03:00
parent 0d572b8fcf
commit 666a37c136
5 changed files with 231 additions and 0 deletions
+53
View File
@@ -0,0 +1,53 @@
"""Request bodies from the lab 1 contract."""
from uuid import UUID
from pydantic import BaseModel, ConfigDict, Field, field_validator
class StrictBody(BaseModel):
model_config = ConfigDict(extra="forbid", strict=True)
class CreateUser(StrictBody):
username: str = Field(pattern=r"^[a-z0-9_]{3,32}$")
display_name: str = Field(min_length=1, max_length=100)
@field_validator("display_name")
@classmethod
def nonblank_name(cls, value: str) -> str:
if not value.strip():
raise ValueError("blank display name")
return value
class CreateChat(StrictBody):
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 nonblank_title(cls, value: str) -> str:
if not value.strip():
raise ValueError("blank title")
return value
@field_validator("member_ids")
@classmethod
def valid_members(cls, value: list[str]) -> list[str]:
if len(set(value)) != len(value):
raise ValueError("duplicate member")
for member_id in value:
UUID(member_id)
return value
class CreateMessage(StrictBody):
text: str = Field(min_length=1, max_length=4000)
@field_validator("text")
@classmethod
def nonblank_text(cls, value: str) -> str:
if not value.strip():
raise ValueError("blank text")
return value