Bird
0
0

How should a REST API handle validation errors for a list of items, for example, errors in each element of a 'products' array?

hard📝 Application Q9 of 15
Rest API - Error Handling
How should a REST API handle validation errors for a list of items, for example, errors in each element of a 'products' array?
A{"errors": {"products": "Invalid list"}}
B{"errors": {"products": {"0": {"name": "Required"}, "1": {"price": "Must be positive"}}}}
C{"errors": {"products": ["Error in list"]}}
D{"errors": {"products": {"name": "Required", "price": "Must be positive"}}}
Step-by-Step Solution
Solution:
  1. Step 1: Understand error reporting for list elements

    Each list element can have its own errors, so errors should be keyed by index.
  2. Step 2: Identify correct JSON structure

    {"errors": {"products": {"0": {"name": "Required"}, "1": {"price": "Must be positive"}}}} uses numeric keys for each product index with nested field errors.
  3. Final Answer:

    {"errors": {"products": {"0": {"name": "Required"}, "1": {"price": "Must be positive"}}}} -> Option B
  4. Quick Check:

    List errors keyed by index with nested field errors [OK]
Quick Trick: Use indexes as keys for list item errors [OK]
Common Mistakes:
  • Using a single message for entire list
  • Not specifying which item has error
  • Flattening list errors without indexes

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Rest API Quizzes