forked from mtusi2026/mtusi2026lab3
Добавить X-Instance-Id, обязательный Idempotency-Key и мягкую остановку API
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
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)
|
||||
Reference in New Issue
Block a user