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, andpassword - The function should call each of the helper validation functions (
validate_name(),validate_email(), andvalidate_password()) - If any check fails, raise a
ValueErrorwith 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, andpassword - Inside, it should call
validate_user()to ensure that the user input is valid - If
validate_user()raises aValueError,register_user()should catch the exception and returnFalse - Otherwise, it should create and return a dictionary with the keys:
name,email, andpassword
How to approach the project
-
Create a user validation function
-
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,emailandpassword
Build the functions logic
- In the function body, use an
ifstatement and call your first helper functionvalidate_name(name) - Check if the output is
Falseby using an equality comparison operator== - If the function returns a
Falsevalue, thenraiseaValueErrorin the body of yourifstatement and pass it a descriptive error message - Repeat this for the helper functions:
validate_email(email)andvalidate_password(password) - If none of the three functions return a
Falsevalue, then return aTruevalue
# 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
defto define a function calledregister_user() - The function should accept parameters:
name,email,password
Input validation and user creation
- Inside the function, use a
tryblock to call thevalidate_user()function, passing in thename,email,andpasswordparameters - If
validate_user()raises aValueError,catch it in theexceptblock and returnFalse,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
usercontaining the following keys:name,email, andpassword - Assign the corresponding function arguments to each key
- Return the
userdictionary, 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")