Robust User Profile Data Validation
In real-world applications, user-provided data is often messy and needs rigorous validation before it can be processed or stored. This challenge focuses on creating a robust validation system for user profile information, ensuring data integrity and preventing common errors.
Problem Description
Your task is to implement a Python function that validates a dictionary representing a user's profile. The validation should check for the presence of required fields, the correctness of data types, and adherence to specific value constraints. This is crucial for maintaining a clean and reliable database and for providing a good user experience by catching input errors early.
Key Requirements:
- Required Fields: The
username,email, andagefields are mandatory. If any of these are missing, the validation should fail. - Data Types:
username: Must be a string.email: Must be a string.age: Must be an integer.interests: Must be a list of strings (can be an empty list).
- Value Constraints:
username: Must be between 3 and 20 characters long, inclusive.email: Must be a valid email format (e.g., contain "@" and a "." after "@"). A basic check is sufficient.age: Must be a positive integer between 0 and 120, inclusive.
- Optional Fields: The
interestsfield is optional. If present, it must conform to the list of strings requirement.
Expected Behavior:
The function should return True if all validation rules are met, and False otherwise.
Edge Cases:
- Empty input dictionary.
- Fields present but with incorrect data types.
- Fields present but failing value constraints.
- Missing required fields.
interestsfield present but not a list or containing non-string elements.
Examples
Example 1:
Input: {
"username": "john_doe",
"email": "john.doe@example.com",
"age": 30,
"interests": ["coding", "hiking"]
}
Output: True
Explanation: All fields are present, have correct data types, and meet value constraints.
Example 2:
Input: {
"username": "JD",
"email": "johndoe@example",
"age": 150
}
Output: False
Explanation: The username is too short. The email is missing a '.' after '@'. The age is out of the valid range.
Example 3:
Input: {
"username": "jane_smith",
"email": "jane.smith@test.org",
"age": 25,
"interests": ["reading", 123]
}
Output: False
Explanation: The 'interests' list contains an element that is not a string (123).
Example 4:
Input: {
"email": "test@example.com",
"age": 40
}
Output: False
Explanation: The required 'username' field is missing.
Example 5:
Input: {}
Output: False
Explanation: The required fields ('username', 'email', 'age') are all missing.
Constraints
- The input will always be a Python dictionary.
- Field values will generally be of basic Python types (string, int, list).
- The email validation can be a simple check for the presence of "@" and "." after "@". No need for a full RFC-compliant email regex.
Notes
Consider creating helper functions for specific validation checks (e.g., email format, age range) to make your main validation function cleaner and more readable. Think about how to handle missing fields gracefully.