refactor: outsources common validation methods

This commit is contained in:
2024-12-08 19:53:33 -03:00
parent 76ef384102
commit 609abb3362
3 changed files with 19 additions and 14 deletions

View File

@@ -8,7 +8,7 @@ from app.utils.alert_type import AlertType
from app.utils.errors.users.user_register_errors import UserRegisterErrors from app.utils.errors.users.user_register_errors import UserRegisterErrors
from app.utils.flash_message import FlashMessage from app.utils.flash_message import FlashMessage
from app.utils.flash_message_category import FlashMessageCategory from app.utils.flash_message_category import FlashMessageCategory
from app.utils.validators import UserValidators from app.utils.validators.user_validators import UserValidators
class UserService: class UserService:
@staticmethod @staticmethod

View File

@@ -1,12 +1,13 @@
from datetime import date from datetime import date
from typing import Optional from typing import Optional
from app.utils.errors.users.user_register_errors import UserRegisterErrors from app.utils.errors.users.user_register_errors import UserRegisterErrors
import re from app.utils.validators.validators import Validators
class UserValidators: class UserValidators:
@staticmethod @staticmethod
def is_valid_user_name(name: Optional[str], isLastname=False): def is_valid_user_name(name: Optional[str], isLastname=False):
if(UserValidators.is_valid_str_and_pattern(name, '^[A-Za-z ]{2,255}$')): if(Validators.is_valid_str_and_pattern(name, '^[A-Za-z ]{2,255}$')):
return name return name
if(isLastname): if(isLastname):
raise UserRegisterErrors("Invalid user lastname") raise UserRegisterErrors("Invalid user lastname")
@@ -14,7 +15,7 @@ class UserValidators:
@staticmethod @staticmethod
def is_valid_birth_date(birth_date: Optional[str]): def is_valid_birth_date(birth_date: Optional[str]):
if(UserValidators.is_valid_str_and_pattern(birth_date, '^\d{4}-\d{2}-\d{2}$')): if(Validators.is_valid_str_and_pattern(birth_date, '^\d{4}-\d{2}-\d{2}$')):
try: try:
birth_date = [int(n) for n in (str(birth_date)).split("-")] birth_date = [int(n) for n in (str(birth_date)).split("-")]
return date(birth_date[0], birth_date[1], birth_date[2]) return date(birth_date[0], birth_date[1], birth_date[2])
@@ -24,13 +25,13 @@ class UserValidators:
@staticmethod @staticmethod
def is_valid_phone_number(phone_number: Optional[str]): def is_valid_phone_number(phone_number: Optional[str]):
if(UserValidators.is_valid_str_and_pattern(phone_number, '^(\+\d{1,3}){0,1}[0-9]{7,255}$')): if(Validators.is_valid_str_and_pattern(phone_number, '^(\+\d{1,3}){0,1}[0-9]{7,255}$')):
return phone_number return phone_number
raise UserRegisterErrors("Invalid user phone number") raise UserRegisterErrors("Invalid user phone number")
@staticmethod @staticmethod
def is_valid_email(email: Optional[str]): def is_valid_email(email: Optional[str]):
if(UserValidators.is_valid_str_and_pattern(email, '^[A-Za-z0-9._%+\-]+@[A-Za-z0-9.\-]+\.[A-Za-z]{2,}$')): if(Validators.is_valid_str_and_pattern(email, '^[A-Za-z0-9._%+\-]+@[A-Za-z0-9.\-]+\.[A-Za-z]{2,}$')):
if(len(email) <= 255): if(len(email) <= 255):
return email return email
raise UserRegisterErrors("Invalid user email") raise UserRegisterErrors("Invalid user email")
@@ -41,14 +42,6 @@ class UserValidators:
return address return address
raise UserRegisterErrors("Invalid user address") raise UserRegisterErrors("Invalid user address")
@staticmethod
def is_valid_str_and_pattern(string: Optional[str], pattern: str):
if(isinstance(string, str)):
regex = re.compile(pattern)
if(regex.match(string) is not None):
return True
return False
@staticmethod @staticmethod
def is_valid_password(password: str): def is_valid_password(password: str):
if(isinstance(password, str)): if(isinstance(password, str)):

View File

@@ -0,0 +1,12 @@
import re
from typing import Optional
class Validators:
@staticmethod
def is_valid_str_and_pattern(string: Optional[str], pattern: str):
if(isinstance(string, str)):
regex = re.compile(pattern)
if(regex.match(string) is not None):
return True
return False