Validating User Data with Pydantic
Pydantic is a powerful Python library for data validation and settings management using Python type annotations. This challenge will task you with defining a Pydantic model to validate user data, ensuring that the data conforms to specific types and constraints before being processed. This is crucial for building robust applications that handle potentially unreliable user input.
Problem Description
You are tasked with creating a Pydantic model called User that represents user data. The User model should have the following fields:
id: An integer representing the user's unique identifier. This field must be positive.username: A string representing the user's username. This field must be between 3 and 20 characters long (inclusive) and can only contain alphanumeric characters (letters and numbers).email: A string representing the user's email address. This field must be a valid email format.age: An integer representing the user's age. This field must be between 18 and 120 (inclusive).is_active: A boolean indicating whether the user account is active.profile: An optional dictionary containing profile information. If present, it should have the following keys:bio: A string representing the user's biography (max 200 characters).location: A string representing the user's location.
Your solution should define the User Pydantic model and then demonstrate its validation capabilities by attempting to validate several example inputs, including valid and invalid data. The validation attempts should print whether the validation was successful and, if not, provide detailed error messages.
Examples
Example 1:
Input:
{
"id": 123,
"username": "john_doe",
"email": "john.doe@example.com",
"age": 30,
"is_active": True
}
Output:
Validation successful.
Explanation: This input contains valid data for all required fields, adhering to the specified constraints.
Example 2:
Input:
{
"id": -10,
"username": "jd",
"email": "invalid-email",
"age": 15,
"is_active": False,
"profile": {"bio": "A short bio", "location": "Anytown"}
}
Output:
Validation errors:
- id: Value must be positive.
- username: Value 'jd' is too short (minimum is 3 characters).
- email: 'invalid-email' does not look like a valid email address.
- age: Value must be between 18 and 120.
Explanation: This input contains several invalid values. The id is negative, the username is too short, the email is invalid, and the age is below the minimum allowed.
Example 3:
Input:
{
"id": 456,
"username": "validUser123",
"email": "another@example.net",
"age": 60,
"is_active": True,
"profile": {"bio": "This is a longer bio that exceeds the character limit.", "location": "Somewhere"}
}
Output:
Validation errors:
- profile: profile.bio: Value 'This is a longer bio that exceeds the character limit.' is too long (maximum is 200 characters).
Explanation: This input is mostly valid, but the bio field within the profile dictionary exceeds the maximum character limit.
Constraints
- All input data will be provided as Python dictionaries.
- The
usernamefield must only contain alphanumeric characters (a-z, A-Z, 0-9). - The email validation should adhere to a reasonable email format.
- The
biofield within theprofiledictionary, if present, must be a string with a maximum length of 200 characters. - The
idmust be a positive integer. - The
agemust be an integer between 18 and 120 (inclusive).
Notes
- Utilize Pydantic's built-in validation features and type annotations.
- Consider using Pydantic's
validatordecorator for custom validation logic, especially for theusernameandprofile.biofields. - Handle validation errors gracefully and provide informative error messages.
- The
profilefield is optional; your model should handle cases where it is not present in the input data. - Focus on creating a robust and well-documented Pydantic model.