31 lines
1.0 KiB
Python
31 lines
1.0 KiB
Python
import re
|
|
from pydantic import EmailStr, model_validator
|
|
from sqlmodel import Field, SQLModel
|
|
|
|
|
|
class UserRegister(SQLModel):
|
|
username: str = Field(min_length=3, max_length=64)
|
|
email: EmailStr = Field()
|
|
phone_number: str = Field(min_length=2, max_length=16, schema_extra={"pattern": r'^\+[1-9]\d{1,14}$'})
|
|
password: str = Field(min_length=8, max_length=128,
|
|
description="Password must be at least 8 and at most 128 characters long, contain a lower case, upper case letter, a number and a special character (#?!@$ %^&*-)")
|
|
|
|
@model_validator(mode="after")
|
|
def validate_using_regex(self):
|
|
self.__validate_password()
|
|
return self
|
|
|
|
def __validate_password(self):
|
|
password_regex = r"^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[#?!@$ %^&*-]).{8,128}$"
|
|
if not re.match(password_regex, self.password):
|
|
raise ValueError("Password is too weak")
|
|
|
|
|
|
class Token(SQLModel):
|
|
access_token: str
|
|
token_type: str = "bearer"
|
|
|
|
|
|
class TokenPayload(SQLModel):
|
|
sub: str | None = None
|