What if your API errors could talk clearly and help users fix problems fast?
Why Validation error response formatting in Express? - Purpose & Use Cases
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine building an API where you check every user input manually and send back error messages in different styles for each endpoint.
Manually formatting error responses is slow, inconsistent, and confusing for clients who consume your API. It's easy to forget details or send unclear messages.
Using a consistent validation error response format ensures all errors look the same, making your API easier to use and debug.
if (!req.body.email) { res.status(400).send('Missing email'); }
res.status(400).json({ errors: [{ field: 'email', message: 'Email is required' }] });
This lets clients handle errors predictably and improves communication between your API and its users.
A signup form that shows clear, consistent error messages for missing or invalid fields, helping users fix mistakes quickly.
Manual error handling is slow and inconsistent.
Formatted validation errors improve clarity and usability.
Consistent responses help clients handle errors easily.
Practice
Solution
Step 1: Understand HTTP status codes for client errors
Validation errors occur when the client sends bad data, so a 4xx code is appropriate.Step 2: Identify the specific code for bad input
400 Bad Request is the standard code for invalid input from the client.Final Answer:
400 Bad Request -> Option AQuick Check:
Validation errors use 400 status [OK]
- Using 200 OK for validation errors
- Confusing 500 Internal Server Error with validation errors
- Using 404 Not Found for input mistakes
Solution
Step 1: Use status 400 for validation errors
The response must have status 400 to indicate a client error.Step 2: Format the response as JSON with an errors array
Returning an object with an errors array containing message objects is the standard pattern.Final Answer:
res.status(400).json({ errors: [{ msg: 'Invalid input' }] }) -> Option CQuick Check:
Use res.status(400).json with errors array [OK]
- Sending plain text instead of JSON
- Omitting the status code
- Using 200 status for errors
app.post('/user', (req, res) => {
const errors = [];
if (!req.body.email) errors.push({ msg: 'Email is required' });
if (errors.length > 0) {
return res.status(400).json({ errors });
}
res.send('User created');
});Solution
Step 1: Check validation logic for missing email
If email is missing, an error object with msg 'Email is required' is added to errors array.Step 2: Return JSON with errors array and status 400
Since errors array is not empty, the response sends status 400 and JSON containing errors array.Final Answer:
{"errors":[{"msg":"Email is required"}]} with status 400 -> Option BQuick Check:
Validation fails -> 400 + errors array JSON [OK]
- Returning success message despite errors
- Wrong JSON key like 'error' instead of 'errors'
- Not setting status 400
if (!req.body.name) {
res.json({ errors: [{ msg: 'Name is required' }] });
res.status(400);
}Solution
Step 1: Check order of status and response methods
res.status(400) must be called before sending the response to set status properly.Step 2: Identify that res.json sends response immediately
Calling res.json first sends the response with default status 200, so status(400) after has no effect.Final Answer:
Status code must be set before sending response -> Option AQuick Check:
Set status before res.json/send [OK]
- Setting status after sending response
- Using res.send instead of res.json (not a bug but less clear)
- Not sending any status code
const errors = [
{ field: 'email', message: 'Invalid email' },
{ field: 'password', message: 'Password too short' }
];
// What is the correct response code?Solution
Step 1: Map errors to standard format with msg and param keys
Express validation errors usually have 'msg' for message and 'param' for field name.Step 2: Send response with status 400 and JSON containing errors array
Use res.status(400).json(...) to send proper status and formatted errors.Final Answer:
res.status(400).json({ errors: errors.map(e => ({ msg: e.message, param: e.field })) }) -> Option DQuick Check:
Map errors to {msg, param} and send with 400 status [OK]
- Sending errors without mapping keys
- Using 200 status for errors
- Using wrong JSON key like errorMessages
