From 82e8fee7f95cbc79234752c05e5ac9945059e88d Mon Sep 17 00:00:00 2001 From: Maksimova Snezhana Date: Wed, 30 Sep 2026 23:59:07 +0300 Subject: [PATCH] =?UTF-8?q?pydantic-=D1=81=D1=85=D0=B5=D0=BC=D1=8B=20?= =?UTF-8?q?=D0=BF=D0=BE=D0=B4=20password=20=D0=B8=20session?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/models.py | 46 ++++++++++++++++++++++++++++++++++++---------- 1 file changed, 36 insertions(+), 10 deletions(-) diff --git a/app/models.py b/app/models.py index 9f612d5..19dc859 100644 --- a/app/models.py +++ b/app/models.py @@ -1,4 +1,5 @@ from uuid import UUID + from pydantic import BaseModel, ConfigDict, Field, field_validator @@ -6,25 +7,50 @@ class CreateUser(BaseModel): model_config = ConfigDict(extra="forbid") 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, v: str) -> str: + if not v.strip(): + raise ValueError("must not be blank") + return v class CreateChat(BaseModel): model_config = ConfigDict(extra="forbid") title: str = Field(min_length=1, max_length=100) - member_ids: list[str] = Field(min_length=1, max_length=100) + member_ids: list[UUID] = Field(min_length=1, max_length=100) + + @field_validator("title") + @classmethod + def _title_not_blank(cls, v: str) -> str: + if not v.strip(): + raise ValueError("title must not be blank") + return v @field_validator("member_ids") @classmethod - def _validate_members(cls, value: list[str]) -> list[str]: - seen: set[str] = set() - for raw in value: - UUID(raw) # бросит ValueError -> RequestValidationError -> 400 - if raw in seen: - raise ValueError("duplicate member_ids") - seen.add(raw) - return value + def _unique_members(cls, v: list[UUID]) -> list[UUID]: + if len(set(v)) != len(v): + raise ValueError("duplicate member_ids") + return v class CreateMessage(BaseModel): model_config = ConfigDict(extra="forbid") - text: str = Field(min_length=1, max_length=4000) \ No newline at end of file + text: str = Field(min_length=1, max_length=4000) + + @field_validator("text") + @classmethod + def _text_not_blank(cls, v: str) -> str: + if not v.strip(): + raise ValueError("text must not be blank") + return v + + +class SessionResponse(BaseModel): + token: str + token_type: str = "Bearer" + expires_at: str + user: dict \ No newline at end of file