Bird
0
0

You want to return multiple validation errors in a REST API error response. Which JSON structure below correctly supports this?

hard📝 Application Q8 of 15
Rest API - Error Handling
You want to return multiple validation errors in a REST API error response. Which JSON structure below correctly supports this?
A{"error": {"code": 422, "message": "Validation failed", "errors": "email and password invalid"}}
B{"error": {"code": 422, "message": "Validation failed", "fields": {"email": "Invalid format", "password": "Too short"}}}
C{"error": {"code": 422, "message": "Validation failed", "fieldErrors": [{"field": "email", "message": "Invalid format"}, {"field": "password", "message": "Too short"}]}}
D{"error": {"code": 422, "message": "Validation failed", "details": "email and password invalid"}}
Step-by-Step Solution
Solution:
  1. Step 1: Understand the need for multiple errors

    Each validation error should be clearly identified with field and message.
  2. Step 2: Analyze options

    {"error": {"code": 422, "message": "Validation failed", "fieldErrors": [{"field": "email", "message": "Invalid format"}, {"field": "password", "message": "Too short"}]}} uses an array of objects with field and message, which is clear and extensible.
  3. Step 3: Evaluate other options

    {"error": {"code": 422, "message": "Validation failed", "fields": {"email": "Invalid format", "password": "Too short"}}} uses a map but lacks message structure; C and D use strings that are less structured.
  4. Final Answer:

    {"error": {"code": 422, "message": "Validation failed", "fieldErrors": [{"field": "email", "message": "Invalid format"}, {"field": "password", "message": "Too short"}]}} provides a structured list of field errors.
  5. Quick Check:

    Is the structure clear and extensible? Yes. [OK]
Quick Trick: Use array of objects for multiple field errors [OK]
Common Mistakes:
  • Using plain strings instead of structured arrays
  • Not associating messages with specific fields
  • Using ambiguous keys like 'details' without structure

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Rest API Quizzes