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

def validate_user(name,email,password):
    """ Validates the user name, email, password.
    
    Args:
        name (str): The inputted name from the user we're attempting to validate.
        email (str): The inputted email from the user we're attempting to validate.
        password (str): The inputted email from the user we're attempting to validate.
        
    Returns:
        bool: Returns True if all validation checks pass.

    Raises:
        ValueError: If any validation check fails.
    
    """
    
    if validate_name(name) == False:
        raise ValueError("Please make sure your name is greater than two characters") 
    elif validate_email(email) == False:
        raise ValueError("Incorrect email format.") 
    elif validate_password(password) == False:
        raise ValueError("Your password is too weak. Make sure it has a capital letter, a number between 0-9 and be greater than 8 characters.")
    else:
        return True

def register_user(name, email, password):
    """ Registers users to the app.
    
    Args:
        name (str): Name of the user.
        email (str): Email address of the user.
        password (str): Password of the user.
    
    Returns:
        dict or bool: Returns a dictionary with the user details if validation is successful,
        or False if the validation fails."""
    
    try: 
        validate_user(name, email, password)
    except ValueError:
        return False
    else:
        user = {'name':name,'email':email,'password':password}
        return user

#TRIAL

username1 = 'bilogcute'
email1 = '[email protected]'
password1 = 'Owomochi123'

validate_user(username, email1, password1)
register_user(username, email1, password1)