Files
mtusi2026lab2-fork/app/schemas.py
T

68 lines
1.9 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=1, max_length=4000)
@field_validator('text')
@classmethod
def _text_not_blank(cls, value: str) -> str:
if not value.strip():
raise ValueError('string must not consist of whitespace only')
return value