0
0
Expressframework~20 mins

Manual validation patterns in Express - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Manual Validation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the response when missing required field in manual validation?
Consider this Express route that manually validates a JSON body for a required username field. What will the server respond if the client sends an empty JSON object {}?
Express
app.post('/register', (req, res) => {
  const { username } = req.body;
  if (!username) {
    return res.status(400).json({ error: 'Username is required' });
  }
  res.status(200).json({ message: `Welcome, ${username}!` });
});
AStatus 200 with JSON { message: 'Welcome, undefined!' }
BStatus 400 with JSON { error: 'Username is required' }
CStatus 500 with server error message
DStatus 404 with empty response
Attempts:
2 left
💡 Hint
Check the condition that tests if username exists before sending success response.
📝 Syntax
intermediate
2:00remaining
Identify the syntax error in this manual validation middleware
This Express middleware is intended to check if email exists in the request body. Which option correctly identifies the syntax error?
Express
function validateEmail(req, res, next) {
  if (!req.body.email) {
    res.status(400).json({ error: 'Email required' });
  } else {
    next();
  }
}
AMissing semicolon after res.status(400).json(...) causes syntax error
BIncorrect use of arrow function syntax
CMissing return statement after sending response causes next() to run anyway
DNo syntax error; code is valid
Attempts:
2 left
💡 Hint
Think about what happens after sending a response without return.
state_output
advanced
2:00remaining
What is the output when validating multiple fields manually?
Given this Express route that manually validates username and password, what JSON response will the client receive if username is provided but password is missing?
Express
app.post('/login', (req, res) => {
  const { username, password } = req.body;
  if (!username) {
    return res.status(400).json({ error: 'Username required' });
  }
  if (!password) {
    return res.status(400).json({ error: 'Password required' });
  }
  res.status(200).json({ message: `User ${username} logged in` });
});
A{ error: 'Password required' } with status 400
B{ error: 'Username required' } with status 400
C{ message: 'User undefined logged in' } with status 200
D{ message: 'User username logged in' } with status 200
Attempts:
2 left
💡 Hint
Check the order of validation checks and which field is missing.
🔧 Debug
advanced
2:00remaining
Why does this manual validation middleware allow invalid emails?
This middleware is supposed to reject requests with invalid email format. Why does it fail to do so?
Express
function checkEmailFormat(req, res, next) {
  const email = req.body.email;
  if (!email || email.indexOf('@') === -1) {
    res.status(400).json({ error: 'Invalid email' });
  } else {
    next();
  }
}
Aemail is undefined when body is empty, causing runtime error
BindexOf('@') returns -1 for valid emails, so condition is wrong
CMiddleware is not attached to any route, so never runs
DMissing return after res.status(400)... causes next() to run even on invalid email
Attempts:
2 left
💡 Hint
What happens after sending a response without stopping execution?
🧠 Conceptual
expert
3:00remaining
Which manual validation pattern best prevents multiple responses in Express?
In manual validation, what is the best pattern to ensure the server sends only one response and does not call next() after sending an error?
AAlways use 'return' before sending a response to stop further code execution
BCall next() immediately after sending a response to continue middleware chain
CUse try-catch blocks around validation and call next() in finally block
DSend multiple responses but handle errors on client side
Attempts:
2 left
💡 Hint
Think about how JavaScript functions stop running after return.