Files
mtusi2026lab4-fork/app/schemas.py

86 lines
2.3 KiB
Python

import uuid as _uuid
from typing import Literal
from pydantic import BaseModel, ConfigDict, Field, field_validator
STRICT = ConfigDict(extra='forbid')
class CreateUser(BaseModel):
model_config = STRICT
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, value: str) -> str:
if not value.strip():
raise ValueError('string must not consist of whitespace only')
return value
class User(BaseModel):
model_config = STRICT
id: _uuid.UUID
username: str
display_name: str
created_at: str
class Session(BaseModel):
model_config = STRICT
token: str
token_type: Literal['Bearer']
expires_at: str
user: User
class CreateChat(BaseModel):
model_config = STRICT
title: str = Field(min_length=1, max_length=100)
member_ids: list[_uuid.UUID] = Field(min_length=1, max_length=100)
@field_validator('title')
@classmethod
def _title_not_blank(cls, value: str) -> str:
if not value.strip():
raise ValueError('string must not consist of whitespace only')
return value
@field_validator('member_ids')
@classmethod
def _unique_members(cls, value):
if len(set(value)) != len(value):
raise ValueError('duplicate member ids')
return value
class CreateMessage(BaseModel):
model_config = STRICT
text: str = Field(min_length=0, max_length=4000)
attachment_ids: list[_uuid.UUID] = Field(default_factory=list, max_length=10)
@field_validator('attachment_ids')
@classmethod
def _unique_attachments(cls, value):
if len(set(value)) != len(value):
raise ValueError('duplicate attachment ids')
return value
class Attachment(BaseModel):
model_config = STRICT
id: _uuid.UUID
chat_id: _uuid.UUID
owner_id: _uuid.UUID
filename: str = Field(min_length=1, max_length=255)
content_type: Literal['image/png', 'image/jpeg']
size_bytes: int = Field(ge=1, le=10_485_760)
sha256: str = Field(pattern=r'^[a-f0-9]{64}$')
status: Literal['ready']
variants: list = Field(default_factory=list)
error_code: str | None = None
created_at: str
updated_at: str