Complete the code to send a JSON error response with status 400.
res.status(400).json({ error: [1] });
The error message must be a string inside quotes to be valid JSON.
Complete the code to extract validation errors from the request and send them in the response.
const errors = req.validationErrors(); if (errors) { return res.status(422).json({ errors: [1] }); }
The variable errors holds the validation errors and should be sent in the response.
Fix the error in the code to properly format the validation error response.
res.status(400).json({ message: [1] });
The message key should have a string describing the error, not the errors array.
Fill both blanks to send a detailed validation error response with status and errors array.
res.status([1]).json({ status: [2], errors: errors.array() });
The HTTP status code 422 means Unprocessable Entity, suitable for validation errors.
Fill all three blanks to create a middleware that checks validation results and sends errors if any.
const { validationResult } = require('express-validator');
function validate(req, res, next) {
const errors = validationResult(req);
if (!errors.isEmpty()) {
return res.status([1]).json({
[2]: [3]
});
}
next();
}Use status 422 for validation errors, and send the errors array under the key 'errors'.