Challenge - 5 Problems
Manual Validation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2: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}!` }); });
Attempts:
2 left
💡 Hint
Check the condition that tests if username exists before sending success response.
✗ Incorrect
The code checks if username is missing and returns a 400 status with an error message. Sending empty JSON means username is undefined, so the error response is sent.
📝 Syntax
intermediate2: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();
}
}Attempts:
2 left
💡 Hint
Think about what happens after sending a response without return.
✗ Incorrect
Without a return after sending the error response, next() will still be called, which can cause unexpected behavior. This is a logic error, not a syntax error, but it is the main issue here.
❓ state_output
advanced2: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` }); });
Attempts:
2 left
💡 Hint
Check the order of validation checks and which field is missing.
✗ Incorrect
Since username is present, the first if is skipped. The second if detects missing password and returns the error JSON with status 400.
🔧 Debug
advanced2: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();
}
}Attempts:
2 left
💡 Hint
What happens after sending a response without stopping execution?
✗ Incorrect
Without return, next() runs even if email is invalid, allowing the request to proceed. This bypasses the intended validation.
🧠 Conceptual
expert3: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?
Attempts:
2 left
💡 Hint
Think about how JavaScript functions stop running after return.
✗ Incorrect
Using 'return' before sending a response stops the function from running further, preventing multiple responses or calling next() after response.