forked from mtusi2026/mtusi2026lab3
46 lines
1.2 KiB
Python
46 lines
1.2 KiB
Python
from pydantic import BaseModel, ConfigDict, Field, field_validator
|
|
|
|
|
|
class StrictModel(BaseModel):
|
|
model_config = ConfigDict(extra='forbid', strict=True)
|
|
|
|
|
|
def not_blank(value):
|
|
if not value.strip():
|
|
raise ValueError('must not be blank')
|
|
try:
|
|
value.encode('utf-8')
|
|
except UnicodeEncodeError:
|
|
raise ValueError('must be valid Unicode') from None
|
|
return value
|
|
|
|
|
|
class CreateUser(StrictModel):
|
|
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_fields(cls, value):
|
|
return not_blank(value)
|
|
|
|
|
|
class CreateChat(StrictModel):
|
|
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 title_not_blank(cls, value):
|
|
return not_blank(value)
|
|
|
|
|
|
class CreateMessage(StrictModel):
|
|
text: str = Field(min_length=1, max_length=4000)
|
|
|
|
@field_validator('text')
|
|
@classmethod
|
|
def text_not_blank(cls, value):
|
|
return not_blank(value)
|