0
0
Rest APIprogramming~15 mins

Validation error details in Rest API - Mini Project: Build & Apply

Choose your learning style9 modes available
Validation error details
📖 Scenario: You are building a simple REST API that accepts user data. You want to check if the data is valid and show clear error messages when it is not.
🎯 Goal: Create a Python dictionary with user data, set a validation rule, check the data against the rule, and print detailed error messages if the data is invalid.
📋 What You'll Learn
Create a dictionary called user_data with keys name and age and exact values 'Alice' and 17
Create a variable called min_age and set it to 18
Use an if statement to check if user_data['age'] is less than min_age and create an error message string called error_message
Print the error_message string
💡 Why This Matters
🌍 Real World
APIs often need to check if user input is correct and show clear error messages to help users fix mistakes.
💼 Career
Understanding validation and error messages is important for backend developers, API designers, and anyone working with user input.
Progress0 / 4 steps
1
Create user data dictionary
Create a dictionary called user_data with keys 'name' and 'age' and values 'Alice' and 17 exactly.
Rest API
Need a hint?

Use curly braces to create a dictionary. Keys are strings and values are 'Alice' and 17.

2
Set minimum age for validation
Create a variable called min_age and set it to the integer 18.
Rest API
Need a hint?

Just assign 18 to the variable min_age.

3
Check age and create error message
Use an if statement to check if user_data['age'] is less than min_age. If true, create a string variable called error_message with the text 'User is too young: age is X' where X is the actual age from user_data.
Rest API
Need a hint?

Use an if statement and an f-string to include the age in the message.

4
Print the error message
Print the variable error_message to show the validation error details.
Rest API
Need a hint?

Use print(error_message) inside the if block.