Bird
Raised Fist0
Rest APIprogramming~20 mins

Validation error details in Rest API - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Validation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this JSON validation error response?
Consider a REST API that validates user input and returns this JSON error response when the 'email' field is missing. What is the output shown to the client?
Rest API
{
  "errors": {
    "email": ["This field is required."]
  },
  "message": "Validation failed"
}
A{"message":"Validation failed","fields":{"email":"required"}}
B{"error":"email is missing","status":400}
C{"errors":{"email":["This field is required."]},"message":"Validation failed"}
D{"status":"error","details":"email missing"}
Attempts:
2 left
💡 Hint
Look for the exact JSON structure that matches the validation error format.
Predict Output
intermediate
1:30remaining
What error message does this API return for invalid password length?
An API validates a password field and returns this JSON error response if the password is too short. What is the exact error message for the password field?
Rest API
{
  "errors": {
    "password": ["Password must be at least 8 characters."]
  },
  "message": "Validation failed"
}
APassword must be at least 8 characters.
BPassword is too short.
CPassword length invalid.
DPassword must contain letters and numbers.
Attempts:
2 left
💡 Hint
Check the exact string inside the password error array.
Predict Output
advanced
2:30remaining
What is the output when multiple fields fail validation?
An API returns this JSON when both 'username' and 'email' fields fail validation. What is the exact JSON output?
Rest API
{
  "errors": {
    "username": ["Username already taken."],
    "email": ["Invalid email format."]
  },
  "message": "Validation failed"
}
A{"error":"Multiple validation errors","fields":{"username":"taken","email":"invalid"}}
B{"errors":{"username":["Username already taken."],"email":["Invalid email format."]},"message":"Validation failed"}
C{"message":"Validation failed","errors":["Username already taken.","Invalid email format."]}
D{"errors":{"username":"Username already taken.","email":"Invalid email format."},"message":"Validation failed"}
Attempts:
2 left
💡 Hint
Look for the JSON where errors are arrays per field.
Predict Output
advanced
1:30remaining
What error does this invalid JSON validation response cause?
This API response is missing a colon after 'errors'. What error will a JSON parser raise?
Rest API
{
  "errors" {
    "email": ["Required field missing."]
  },
  "message": "Validation failed"
}
AJSONDecodeError: Expecting ':' delimiter
BKeyError: 'errors'
CTypeError: string indices must be integers
DNo error, valid JSON
Attempts:
2 left
💡 Hint
Check the syntax near the 'errors' key.
🧠 Conceptual
expert
3:00remaining
Which option correctly describes the structure of detailed validation error responses in REST APIs?
In REST API design, detailed validation error responses typically include which of the following structures?
AA flat list of error strings without field association.
BA single error string summarizing all validation issues.
CAn array of objects each containing a 'code' and 'description' but no field names.
DAn object with a 'message' string and an 'errors' object mapping field names to arrays of error messages.
Attempts:
2 left
💡 Hint
Think about how clients identify which fields failed validation.

Practice

(1/5)
1. What is the main purpose of validation error details in a REST API response?
easy
A. To explain which input fields are wrong and why
B. To speed up the server response time
C. To encrypt the data sent to the client
D. To log user activity for analytics

Solution

  1. Step 1: Understand validation errors

    Validation errors tell us what input data is incorrect or missing.
  2. Step 2: Purpose of error details

    Error details explain which fields caused the problem and why, helping fix input.
  3. Final Answer:

    To explain which input fields are wrong and why -> Option A
  4. Quick Check:

    Validation error details = Explain input errors [OK]
Hint: Validation errors show what and why input is wrong [OK]
Common Mistakes:
  • Thinking error details speed up server
  • Confusing error details with encryption
  • Assuming error details are for logging only
2. Which of the following is the correct JSON structure for validation error details in a REST API?
easy
A. {"data": {"email": "user@example.com"}}
B. ["email", "password"]
C. {"status": 200, "message": "Success"}
D. {"errors": {"email": "Invalid format", "password": "Too short"}}

Solution

  1. Step 1: Identify error details format

    Error details usually use a JSON object with field names as keys and error messages as values.
  2. Step 2: Check options

    {"errors": {"email": "Invalid format", "password": "Too short"}} shows a JSON object with "errors" key and field-specific messages, which is correct.
  3. Final Answer:

    {"errors": {"email": "Invalid format", "password": "Too short"}} -> Option D
  4. Quick Check:

    Validation errors = JSON object with field messages [OK]
Hint: Look for JSON object with field names and error messages [OK]
Common Mistakes:
  • Using arrays instead of objects for errors
  • Confusing success response with error details
  • Missing field names in error messages
3. Given this JSON error response:
{"errors": {"username": "Required field", "age": "Must be a number"}}

What message should the client show for the 'age' field?
medium
A. Must be a number
B. Invalid email format
C. Required field
D. Password too short

Solution

  1. Step 1: Read the JSON error response

    The error for "age" is "Must be a number".
  2. Step 2: Match the message to the field

    The client should show the message exactly as given for the "age" field.
  3. Final Answer:

    Must be a number -> Option A
  4. Quick Check:

    Field 'age' error = Must be a number [OK]
Hint: Match field name to its error message in JSON [OK]
Common Mistakes:
  • Mixing error messages between fields
  • Showing unrelated error messages
  • Ignoring the JSON structure
4. You receive this validation error JSON:
{"error": "Invalid input", "fields": ["email", "password"]}

What is wrong with this error detail format?
medium
A. It correctly shows error details
B. It uses a list instead of mapping fields to messages
C. It uses wrong HTTP status code
D. It encrypts error messages

Solution

  1. Step 1: Analyze error detail format

    The "fields" key has a list of field names but no messages explaining the errors.
  2. Step 2: Identify correct format

    Validation error details should map each field to a specific error message, not just list fields.
  3. Final Answer:

    It uses a list instead of mapping fields to messages -> Option B
  4. Quick Check:

    Error details need field-message pairs [OK]
Hint: Error details need field names paired with messages [OK]
Common Mistakes:
  • Listing fields without error explanations
  • Assuming any error JSON is correct
  • Ignoring the need for descriptive messages
5. You want to design a REST API validation error response that shows multiple errors per field. Which JSON structure correctly supports this?
hard
A. {"errors": ["email: Required", "password: Too short"]}
B. {"error": "Multiple errors found"}
C. {"errors": {"email": ["Required", "Invalid format"], "password": ["Too short"]}}
D. {"fields": ["email", "password"]}

Solution

  1. Step 1: Understand multiple errors per field

    To show many errors for one field, use a list (array) of messages for that field.
  2. Step 2: Check JSON structures

    {"errors": {"email": ["Required", "Invalid format"], "password": ["Too short"]}} uses a dictionary with field keys and lists of error messages, which fits the need.
  3. Final Answer:

    {"errors": {"email": ["Required", "Invalid format"], "password": ["Too short"]}} -> Option C
  4. Quick Check:

    Multiple errors per field = list of messages [OK]
Hint: Use lists inside error fields to show multiple messages [OK]
Common Mistakes:
  • Using flat lists without field keys
  • Not supporting multiple messages per field
  • Using vague error messages without details