diff --git a/src/schemas/users.py b/src/schemas/users.py index 02f8f1c..06bd51e 100644 --- a/src/schemas/users.py +++ b/src/schemas/users.py @@ -3,7 +3,7 @@ from typing import Annotated from pydantic import BaseModel, BeforeValidator, ConfigDict, EmailStr from src.schemas.tasks import TaskWithId -from src.schemas.validators import ensure_password +from src.schemas.validators import ensure_password, ensure_username class UserUpdate(BaseModel): @@ -38,7 +38,7 @@ class UserRequest(BaseModel): class UserRequestADD(BaseModel): - username: str + username: Annotated[str, BeforeValidator(ensure_username)] email: EmailStr | None = None password: Annotated[str, BeforeValidator(ensure_password)] diff --git a/src/schemas/validators.py b/src/schemas/validators.py index 5910daf..32db8f6 100644 --- a/src/schemas/validators.py +++ b/src/schemas/validators.py @@ -1,11 +1,21 @@ from typing import Any -def ensure_password(value: Any) -> Any: +def ensure_password(value: Any) -> str: if not isinstance(value, str): raise TypeError("Password must be a string") + value = value.strip() if len(value) < 8: raise ValueError("Password must be at least 8 characters") - if value.strip() == "": + elif value.strip() == "": raise ValueError("Password cannot be empty") return value + + +def ensure_username(value: str) -> str: + value = value.strip() + if len(value) < 3: + raise ValueError("Username must be at least 3 characters") + elif value.lower() in ['admin', 'moderator', 'админ', 'модератор']: + raise ValueError("Login is already taken") + return value \ No newline at end of file