0
0
Expressframework~10 mins

Validation error response formatting in Express - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to send a JSON error response with status 400.

Express
res.status(400).json({ error: [1] });
Drag options to blanks, or click blank then click option'
A"Invalid input"
BInvalid input
C400
Derror
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting quotes around the error message string.
Passing a number instead of a string as the error message.
2fill in blank
medium

Complete the code to extract validation errors from the request and send them in the response.

Express
const errors = req.validationErrors();
if (errors) {
  return res.status(422).json({ errors: [1] });
}
Drag options to blanks, or click blank then click option'
Areq.errors
Berror
Cerrors
DvalidationErrors
Attempts:
3 left
💡 Hint
Common Mistakes
Using a wrong variable name that does not exist.
Sending a single error instead of the whole errors array.
3fill in blank
hard

Fix the error in the code to properly format the validation error response.

Express
res.status(400).json({ message: [1] });
Drag options to blanks, or click blank then click option'
Aerror
B"Validation failed"
Cerrors.array()
Derrors
Attempts:
3 left
💡 Hint
Common Mistakes
Passing an array where a string is expected.
Using a variable that is not defined.
4fill in blank
hard

Fill both blanks to send a detailed validation error response with status and errors array.

Express
res.status([1]).json({ status: [2], errors: errors.array() });
Drag options to blanks, or click blank then click option'
A422
B400
Attempts:
3 left
💡 Hint
Common Mistakes
Using 400 instead of 422 for validation errors.
Mismatch between HTTP status and JSON status field.
5fill in blank
hard

Fill all three blanks to create a middleware that checks validation results and sends errors if any.

Express
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();
}
Drag options to blanks, or click blank then click option'
A400
Berrors
Cerrors.array()
D422
Attempts:
3 left
💡 Hint
Common Mistakes
Using status 400 instead of 422.
Sending the errors object directly instead of errors.array().