uservalidation
A small Python exercise focused on validating user input before registration. The function checks a name, an email address and a password by delegating each validation rule to smaller helper functions.
What this function does
The goal is deliberately simple: keep the registration flow clean by separating validation from user creation. First, validate_user() checks the three input fields. Then, register_user() returns a user dictionary only if every validation step succeeds.
- Name: must pass
validate_name(name). - Email: must pass
validate_email(email). - Password: must pass
validate_password(password). - Failure behavior: validation errors are converted into
Falseduring registration.
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
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
Development notes
This is a useful first pattern because it introduces a common backend idea: validation should happen before persistence or account creation. In a larger application, the same structure could be extended with typed exceptions, password hashing and database storage.
One future improvement would be replacing the broad except: block with
except ValueError:, so unexpected errors are not silently swallowed.