Files
sqlalchemy-fastapi-pydentic…/server/backend/pydentic.py
2025-09-19 18:17:31 +03:00

40 lines
2.0 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from pydantic import BaseModel, Field, EmailStr, constr,validator
from typing import List, Optional
from enum import IntEnum
#Валидация пароля
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")
@validator('password')
def password_validator(cls, password):
return check_password_complexity(cls, password)
class IdofPersons(UsersInfo):
id:int = Field(..., description="Unique identifier of the user")
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")
@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")