Source code for util.customer_data_validator
import re
[docs]class CustomerDataValidator:
[docs] def __init__(self, application_dependencies):
"""
Class to handle the validation
Args:
application_dependencies (util.application_dependencies.ApplicationDependencies): Application dependency object used for DI
"""
self.application_dependencies = application_dependencies
[docs] def validate_email(self, email):
"""
Method to validate an email
Args:
email (str): The email string to check
Returns:
bool: True or False if the email was valid or not.
"""
logger = self.application_dependencies.get_default_logger()
try:
logger.debug("Attempting to validate email")
return bool(re.match(r"^(?!.*(?:\.\.))[^@\s]+@[^@\s\.]+\.[^@\s]+$", email))
except TypeError as e:
logger.error(
"The email to send to the notification to is not the correct type. Error: {error}".format(error=str(e)))
logger.error("The email to send the notification to is not valid")
return False
[docs] def validate_uk_mobile_number(self, number):
"""
Method to validate a mobile number with the United Kingdom Area code
Args:
number (str): Number as string not including '+'
Returns:
bool: True or false if the number is valid and has leading 44
"""
logger = self.application_dependencies.get_default_logger()
number = str(number)
logger.debug("Attempting to validate mobile number to send SMS")
match_regex = bool(re.match(r"^(?:447)[0-9]{9}|^(?:07)[0-9]{9}$", number))
if match_regex:
return True
else:
logger.error("The number to send the SMS notification to is not valid")
return False