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
Recall & Review
beginner
What is the purpose of formatting validation error responses in Express?
To provide clear, consistent, and user-friendly error messages that help clients understand what went wrong and how to fix it.
Click to reveal answer
beginner
Which HTTP status code is commonly used for validation errors in Express?
HTTP 400 Bad Request is commonly used to indicate validation errors because the client sent invalid data.
Click to reveal answer
intermediate
What is a good JSON structure for sending validation errors in Express?
A good structure includes a clear message and a list of errors with details, for example: { "message": "Validation failed", "errors": [{ "field": "email", "error": "Invalid format" }] }
Click to reveal answer
intermediate
How can you capture validation errors in Express middleware?
You can use middleware like express-validator to check inputs and then handle errors in a centralized error handler that formats the response.
Click to reveal answer
beginner
Why should validation error responses be consistent across an API?
Consistency helps frontend developers and API consumers handle errors easily and improves the user experience by providing predictable feedback.
Click to reveal answer
Which status code should you use for validation errors in Express?
A200 OK
B400 Bad Request
C500 Internal Server Error
D404 Not Found
✗ Incorrect
400 Bad Request indicates the client sent invalid data, which is appropriate for validation errors.
What key should you include in a JSON validation error response to list specific problems?
Atoken
Bdata
Csuccess
Derrors
✗ Incorrect
The 'errors' key typically holds an array of detailed validation issues.
Which Express middleware is commonly used for input validation?
Aexpress-validator
Bbody-parser
Ccors
Dhelmet
✗ Incorrect
express-validator provides easy validation and sanitization of request data.
What is a benefit of formatting validation errors clearly?
AHides error details
BMakes server slower
CHelps clients fix their input mistakes
DConfuses users
✗ Incorrect
Clear error messages guide users to correct their input.
Where should you handle validation errors in an Express app?
AIn a centralized error-handling middleware
BOnly in route handlers
CIn the database layer
DIn the frontend code
✗ Incorrect
Centralized error middleware keeps error handling consistent and clean.
Explain how to format a validation error response in Express to help API users understand what went wrong.
Think about how you would tell a friend exactly what input they need to fix.
You got /4 concepts.
Describe how middleware like express-validator helps with validation error handling in Express.
Middleware acts like a checkpoint before your main code runs.
You got /4 concepts.
Practice
(1/5)
1. What HTTP status code is typically used to indicate validation errors in an Express API response?
easy
A. 400 Bad Request
B. 200 OK
C. 500 Internal Server Error
D. 404 Not Found
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 A
Quick Check:
Validation errors use 400 status [OK]
Hint: Validation errors always use 400 status code [OK]
Common Mistakes:
Using 200 OK for validation errors
Confusing 500 Internal Server Error with validation errors
Using 404 Not Found for input mistakes
2. Which of the following is the correct way to send a JSON validation error response in Express?
easy
A. res.status(400).send('Validation failed')
B. res.json({ error: 'Validation error' })
C. res.status(400).json({ errors: [{ msg: 'Invalid input' }] })
D. res.sendStatus(200)
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.
Hint: Use res.status(400).json({ errors: [...] }) for validation errors [OK]
Common Mistakes:
Sending plain text instead of JSON
Omitting the status code
Using 200 status for errors
3. Given this Express route snippet, what will be the JSON response if the input validation fails?
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');
});
medium
A. {"error":"Email missing"} with status 400
B. {"errors":[{"msg":"Email is required"}]} with status 400
C. "User created" with status 200
D. Empty response with status 400
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 B
Quick Check:
Validation fails -> 400 + errors array JSON [OK]
Hint: Errors array with messages sent with 400 status [OK]
Common Mistakes:
Returning success message despite errors
Wrong JSON key like 'error' instead of 'errors'
Not setting status 400
4. Identify the bug in this Express validation error response code:
if (!req.body.name) {
res.json({ errors: [{ msg: 'Name is required' }] });
res.status(400);
}
medium
A. Status code must be set before sending response
B. Response should use res.send instead of res.json
C. Errors array should be a string, not an object
D. No bug, code is correct
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 A
Quick Check:
Set status before res.json/send [OK]
Hint: Call res.status before res.json/send [OK]
Common Mistakes:
Setting status after sending response
Using res.send instead of res.json (not a bug but less clear)
Not sending any status code
5. You want to send multiple validation errors in a consistent format in Express. Which code snippet correctly formats and sends these errors with HTTP 400 status?
const errors = [
{ field: 'email', message: 'Invalid email' },
{ field: 'password', message: 'Password too short' }
];
// What is the correct response code?
hard
A. res.status(400).json({ errorMessages: errors })