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

from python_functions import validate_name, validate_email, validate_password, top_level_domains

# ----------------------------------------
# define function: validate_user
# ----------------------------------------

def validate_user(name, email, password):
    """
    Validate user input (name, email, and password) on the sign-up form before creating an account.
    
    Args: 
        name (str): The inputted name from the user.
        email (str): The inputted email from the user.
        password (str): The inputted password from the user.
    
    Returns: 
        bool: True if all input (name, email, and password) pass the checks, False otherwise.
    """
    
    # call the helper validation function to check name 
    if not validate_name(name): 
        raise ValueError("Name must be greater than two characters and be a string data type.")
    
    # call the helper validation function to check email 
    if not validate_email(email): 
        raise ValueError("Email address must be in a valid format, has a username greater than 1 character, an '@' symbol, and an allowed domain that is in this list ['.org', '.net', '.edu', '.ac', '.uk', '.com'].") 
    
    # call the helper validation function to check password 
    if not validate_password(password): 
        raise ValueError("Password must be strong enough. It should include a capital letter, a number between 0-9 and be greater than 8 characters.")
    
    return True 

# ----------------------------------------
# test the function: validate_user 
# ----------------------------------------

# validate_user('Totti', '[email protected]', 'Abcdefgh123')
# validate_user(123456, '[email protected]', 'Abcdefgh123')
# validate_user('Totti', '[email protected]', 'Abcdefgh123')
# validate_user('Totti', '[email protected]', 'abcdefghijk')

# ----------------------------------------
# define function: register_user
# ----------------------------------------

def register_user(name, email, password): 
    """
    Validate that all the user details (name, email, and password) are correct, and then return the new user dictionary
    
    Args: 
        name (str): The inputted name from the user.
        email (str): The inputted email from the user.
        password (str): The inputted password from the user.
    
    Returns: 
        new_user (dict): new user dictionary with the keys: name, email, password.
        or bool: if validation is failed. 
    """
    
    # call function validate_user to ensure that user input is valid
    bool_validation = validate_user(name, email, password)
    
    # if all check pass, create a dictionary with the keys: name, email, password 
    dict_new_user = {} 
    if bool_validation: 
        dict_new_user['name'] = name
        dict_new_user['email'] = email 
        dict_new_user['password'] = password 
        # return the new user dictionary  
        return dict_new_user 
    else: 
        # return boolean value of False 
        return False 

# ----------------------------------------
# test the function: register_user 
# ----------------------------------------

# register_user('Totti', '[email protected]', 'Abcdefgh123')
# register_user(123456, '[email protected]', 'Abcdefgh123')
# register_user('Totti', '[email protected]', 'Abcdefgh123')
# register_user('Totti', '[email protected]', 'abcdefghijk')

valid_user = register_user('Francesco Totti', '[email protected]', 'StrongPassword123!')
# valid_user = register_user('Francesco Totti', '[email protected]', 'StrongPassword123!')

if valid_user:
    print("User registration successful:", valid_user)
else:
    print("User registration failed due to invalid input.")

# ----------------------------------------

# ----------------------------------------