0
0
Expressframework~20 mins

Validation error response formatting in Express - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Express Validation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this Express validation error handler?
Consider this Express middleware that handles validation errors. What JSON response will the client receive if a validation error occurs?
Express
app.use((err, req, res, next) => {
  if (err.isValidationError) {
    return res.status(400).json({
      error: {
        message: err.message,
        fields: err.fields
      }
    });
  }
  next(err);
});
A{"error":{"message":"Invalid input","fields":{"email":"Invalid format"}}}
B{"message":"Invalid input","fields":{"email":"Invalid format"}}
C{"error":"Invalid input","fields":{"email":"Invalid format"}}
D{"status":400,"error":"Invalid input"}
Attempts:
2 left
💡 Hint
Look at the structure inside the json() call and how the error object is nested.
📝 Syntax
intermediate
2:00remaining
Which option correctly formats a validation error response in Express?
You want to send a 422 status with a JSON body containing an error message and an array of invalid fields. Which code snippet is correct?
Ares.status(422).send({ error: { message: "Invalid data", fields: ["name", "email"] } });
Bres.status(422).json({ message: "Invalid data", fields: ["name", "email"] });
Cres.status(422).json({ error: { message: "Invalid data", fields: ["name", "email"] } });
Dres.json(422, { error: { message: "Invalid data", fields: ["name", "email"] } });
Attempts:
2 left
💡 Hint
Check the correct order of method chaining and JSON response method in Express.
🔧 Debug
advanced
2:00remaining
Why does this validation error response not send the expected JSON?
This Express middleware tries to send a validation error response but the client receives an empty response. What is the issue?
Express
app.use((err, req, res, next) => {
  if (err.isValidationError) {
    res.status(400);
    res.json({ error: err.message });
    return;
  }
  next(err);
});
AThe middleware calls next(err) after sending a response, causing Express to override the response with an empty one.
Bres.status(400) is not chained with res.json(), so status code is ignored.
CThe error object is missing a fields property, so JSON is empty.
DMiddleware must end with return statement to send response properly.
Attempts:
2 left
💡 Hint
Think about what happens when next() is called after sending a response.
state_output
advanced
2:00remaining
What is the status code and JSON response sent by this validation error handler?
Given this Express error handler, what status code and JSON does the client receive when a validation error occurs?
Express
app.use((err, req, res, next) => {
  if (err.isValidationError) {
    res.status(422).json({
      error: {
        message: err.message,
        invalidFields: err.invalidFields
      }
    });
  } else {
    res.status(500).json({ error: "Internal Server Error" });
  }
});
AStatus: 500, JSON: {"error":"Internal Server Error"}
BStatus: 400, JSON: {"error":{"message":"Missing email","invalidFields":["email"]}}
CStatus: 422, JSON: {"error":"Missing email"}
DStatus: 422, JSON: {"error":{"message":"Missing email","invalidFields":["email"]}}
Attempts:
2 left
💡 Hint
Check the status code set for validation errors and the JSON structure.
🧠 Conceptual
expert
3:00remaining
Which statement about validation error response formatting in Express is true?
Select the correct statement about best practices for formatting validation error responses in Express applications.
AIt is best to send multiple separate responses for each validation error field to keep responses simple.
BValidation error responses should always include a consistent error object with message and details to help clients handle errors properly.
CValidation errors should use HTTP status code 200 with an error field in the JSON to avoid client-side exceptions.
DSending plain text error messages is preferred over JSON for validation errors to reduce response size.
Attempts:
2 left
💡 Hint
Think about how clients consume error responses and what helps them handle errors effectively.