Files
sqlalchemy-fastapi-pydentic…/server/backend/pydentic.py
T
2025-10-12 15:00:23 +03:00

54 lines
2.8 KiB
Python

from pydantic import BaseModel, Field, EmailStr, constr, field_validator
from typing import List, Optional
from enum import IntEnum
from datetime import datetime
#Валидация пароля
import re
def check_password_complexity(cls, password): #Проверка пароля на соответствие сложности
if password is None:
return password
if not re.search(r'[A-Za-z]', password):
raise ValueError('Password must contain at least one letter')
if not re.search(r'\d', password):
raise ValueError('Password must contain at least one digit')
if not re.search(r'[!@#$%^&*(),.?":{}|<>]', password):
raise ValueError('Password must contain at least one special symbol')
return password
#Валидация полей с пользователями
class UsersInfo(BaseModel):
email:EmailStr = Field(..., min_length=6, max_length=254, description="email of the user")
description: str = Field(..., description="description of the user")
activated:bool = Field(..., description="Has the user activated their account")
password:constr(min_length=8) = Field(..., description="Password with min 8 chars, letters and digits")
@field_validator('password')
def password_validator(cls, password):
return check_password_complexity(cls, password)
class CreateUser(UsersInfo):
pass
class UserUpdate(BaseModel):
email:Optional[EmailStr] = Field(None, min_length=6, max_length=254, description="users' email")
description:Optional[str] = Field(None, description="description of the user")
activated:Optional[bool] = Field(None, description="Has the user activated their account")
password:Optional[constr(min_length=8)] = Field(None, description="Password with min 8 chars, letters and digits")
can_edit:Optional[bool] = Field(None, description="The user can edit something")
can_delete:Optional[bool] = Field(None, description="The user can delete something")
can_view:Optional[bool]=Field(None, description="The user can view something")
@field_validator('password')
def password_validator(cls, password):
return check_password_complexity(cls, password)
class UserLogin(BaseModel):
email:EmailStr = Field(..., min_length=6, max_length=254, description="user's email")
password:str = Field(..., description="Password")
class UserReset(BaseModel):
email:EmailStr = Field(..., min_length=6, max_length=254, description="user's email")
new_password:constr(min_length=8) = Field(None,description="New_password")
@field_validator('new_password')
def password_validator(cls, new_password):
return check_password_complexity(cls, new_password)
class UserOut(BaseModel):
email:EmailStr
description:str
activated:bool
created_at:datetime