Skip to content

You are a junior developer working in a small start-up. Your managers have asked you to develop a new account registration system for a mobile app. The system must validate user input on the sign-up form before creating an account.

The previous junior developer wrote some helper functions that validate the name, email, and password. Use these functions to register users, store their data, and implement some error handling! These have been imported into the workspace for you. They will be a great help to you when registering the user, but first you have to understand what the function does! Inspect the docstrings of each of the helper functions: validate_name, validate_email and validate_password.

# Re-run this cell and examine the docstring of each function
from python_functions import validate_name, validate_email, validate_password, top_level_domains

print("validate_name\n")
print(validate_name.__doc__)
print("--------------------\n")

print("validate_email\n")
print(validate_email.__doc__) 
print("--------------------\n")

print("validate_password\n")
print(validate_password.__doc__)

# The top level domains variable is used in validate_email to approve only certain email domains
print(top_level_domains)
# Start coding here
# Use as many cells as you need

def validate_user(name, email, password):
    """Checks if the inputted user details meets the required criteria.
    
    Args: 
        Args:
        name (str): The user's name to validate.
        email (str): The user's email to validate.
        password (str): The user's password to validate.
    
    Returns:
        bool: `True` if all validations pass.
        
    Raises:
        ValueError: If any of the validations fail, with a message explaining the specific issue.
    """
    errors = []
    
    if not validate_name(name):
        errors.append(f"Name: {name} is too short. It should be a single word with more than two characters.")
        
    if not validate_email(email):
        errors.append(f"Email: {email} should be in a valid format. It should have a username greater than 1 character, an '@' symbol, and from one of these domains: {top_level_domains}")
    if not validate_password(password):
        errors.append(f"Password: {password} is not strong enough. It should include a capital letter, a number between 0-9 and be greater than 8 characters.")
    if errors:
        raise ValueError("Validation Failed with these error/s:\n"+ "\n".join(errors))
    return True
    
        
# validate_user('Peter', 'example.com', 'verYsecurepassword345')
def register_user(name, email, password):
    # details_dict = {}
    """
    Registers users using their name, email, and password. Uses validate_user helper function to check if user details are valid.
    
    Args:
        (str): The inputted details (name, email, password)
        
    Returns:
        (dict): A dictionary of user details collected from the user input, False otherwise.
    """
    if validate_user(name, email, password):
        return {key: value for key, value in [('name', name), ('email', email), ('password', password)]}

    # if validate_user(name, email, password) == True:
    #     details_dict['name'] = name
    #     details_dict['email'] = email
    #     details_dict['password'] = password
    #     return details_dict
    else:
        return False
register_user('Mohito', '[email protected]', 'Utejh0_3er')
    
help(validate_user)