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.

Create a validate_user() function, using some helper validation functions to verify user input.

  • The function should take in the parameters: name, email, and password
  • The function should call each of the helper validation functions (validate_name(), validate_email(), and validate_password())
  • If any check fails, raise a ValueError with a descriptive error message about the failing validation
  • If all checks pass, return True

Now that you've validated that all the user details are correct, you want to allow users to register to the app. Create a register_user() function to handle the registration logic.

  • The function should take in the parameters: name, email, and password
  • Inside, it should call validate_user() to ensure that the user input is valid
  • If validate_user() raises a ValueError, register_user() should catch the exception and return False
  • Otherwise, it should create and return a dictionary with the keys: name, email, and password

How to approach the project

  1. Create a user validation function

  2. Create a user registration function

1. Create a user validation function

You will create a function validating user input using some helper functions. It will also raise errors, with meaningful feedback messages to inform users which input has failed the checks.

Define the function

  • Use the def keyword to define a function called validate_user()
  • The function should accept three parameters: name, email and password

Build the functions logic

  • In the function body, use an if statement and call your first helper function validate_name(name)
  • Check if the output is False by using an equality comparison operator ==
  • If the function returns a False value, then raise a ValueError in the body of your if statement and pass it a descriptive error message
  • Repeat this for the helper functions: validate_email(email) and validate_password(password)
  • If none of the three functions return a False value, then return a True value
# 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)
from python_functions import validate_name, validate_email, validate_password

def validate_user(name, email, password):
    """Validate the user name, email and password.

    Args:
        name (string): Name that we're attempting to validate.
        email (string): Email address that we're attempting to validate.
        password (string): Password that 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 2 characters!")

    if validate_email(email) == False:
        raise ValueError("Your email address is in the incorrect format, please enter a valid email.")

    if validate_password(password) == False:
        raise ValueError("Your password is too weak, ensure that your password is greater than 8 characters, "
                         "contains a capital letter and a number.")

    return True
# test 1
validate_user("First Last Name", "[email protected]", "password1234")
# test 2
validate_user("Te", "[email protected]", "Password1234")
# test 3
validate_user("First Last Name", "[email protected]", "Password1234")
# test 4
validate_user("First Last Name", "[email protected]", "Password1234")

2. Create a user registration function

You will create a function that validates the user input. If the validation passes, it will then create a new user dictionary.

Define the function

  • Use def to define a function called register_user()
  • The function should accept parameters: name, email, password

Input validation and user creation

  • Inside the function, use a try block to call the validate_user() function, passing in the name, email, and password parameters
  • If validate_user() raises a ValueError, catch it in the except block and return False, indicating that registration failed due to invalid input

Registering valid user

  • If validate_user() does not raise an error (i.e., the input is valid), proceed to create a dictionary with the user details
  • Define a dictionary named user containing the following keys: name, email, and password
  • Assign the corresponding function arguments to each key
  • Return the user dictionary, indicating successful registration
def register_user(name, email, password):
    """Attempt to register the user if they pass validation.

    Args:
        name (string): Name of the user.
        email (string): Email address of the user.
        password (string): 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:
        return False

    user = {
        "name": name,
        "email": email,
        "password": password
    }

    return user
# test 1
register_user("Last First", "[email protected]", "_assword8765")
# test 2
register_user("23", "[email protected]", "_assword8765")