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:
Step 1: Understand error reporting for list elements
Each list element can have its own errors, so errors should be keyed by index.
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.
Final Answer:
{"errors": {"products": {"0": {"name": "Required"}, "1": {"price": "Must be positive"}}}} -> Option B
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
Master "Error Handling" in Rest API
9 interactive learning modes - each teaches the same concept differently