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)
# User validation function
def validate_user(name, email, password):
    """
    Validates the user's name, email, and password using helper functions.

    Parameters:
    name (str): The user's name to be validated.
    email (str): The user's email to be validated.
    password (str): The user's password to be validated.

    Returns:
    bool: True if all validations pass.

    Raises:
    ValueError: If any of the validations fail with a specific error message.
    """
    # Validate the user's name
    if validate_name(name) == False:
        raise ValueError("Sorry, who are you?")
    
    # Validate the user's email
    if validate_email(email) == False:
        raise ValueError("Oops, wrong email.")
    
    # Validate the user's password
    if validate_password(password) == False:
        raise ValueError("Nope, try again!")
    
    # Return True if all validations pass
    return True
# User registration function
def register_user(name, email, password):
    """
    Registers a user by validating their name, email, and password.

    Parameters:
    name (str): The user's name to be registered.
    email (str): The user's email to be registered.
    password (str): The user's password to be registered.

    Returns:
    dict: A dictionary containing the user's name, email, and password if registration is successful.
    bool: False if registration fails due to validation errors.
    """
    # Validate the user's name, email, and password
    if validate_user(name, email, password):
        # Create a user dictionary with the validated information
        user = {"name": name, "email": email, "password": password}
        return user
    
    # Return False if validation fails
    return False