Challenge - 5 Problems
Express Validation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output of this Express middleware with custom validation?
Consider this Express middleware that validates a request body property 'age' to be a number greater than 18. What response will the server send if the request body is {"age": 16}?
Express
app.post('/check-age', (req, res, next) => { const age = req.body.age; if (typeof age !== 'number' || age <= 18) { return res.status(400).json({ error: 'Age must be a number greater than 18' }); } next(); }, (req, res) => { res.json({ message: 'Age is valid' }); });
Attempts:
2 left
💡 Hint
Think about the condition that checks the age value and what happens when it fails.
✗ Incorrect
The middleware checks if age is not a number or less than or equal to 18. Since 16 is less than 18, it returns a 400 status with the error message.
📝 Syntax
intermediate2:00remaining
Which option correctly defines a custom validation function in Express?
You want to create a custom validation function to check if a string is uppercase. Which code snippet correctly defines and uses this function in Express middleware?
Attempts:
2 left
💡 Hint
Check if the function returns a boolean and compares correctly.
✗ Incorrect
Option A correctly returns true if the string equals its uppercase version. Other options either don't return a value or compare incorrectly.
🔧 Debug
advanced2:00remaining
Why does this custom validation middleware always pass even with invalid input?
This middleware is supposed to reject requests where 'email' is missing or invalid. Why does it allow invalid emails?
Express
app.use((req, res, next) => {
const email = req.body.email;
if (!email && !email.includes('@')) {
return res.status(400).send('Invalid email');
}
next();
});Attempts:
2 left
💡 Hint
Look carefully at the if condition combining !email and !email.includes('@').
✗ Incorrect
The condition uses &&, so it only triggers if email is falsy AND email.includes('@') is false. But if email is falsy, email.includes('@') is not checked, so the condition fails to catch missing or invalid emails.
❓ state_output
advanced2:00remaining
What is the value of req.customData after this validation middleware runs?
This middleware adds a customData property to req if validation passes. What is req.customData after a request with body { username: 'User123' }?
Express
app.use((req, res, next) => {
const username = req.body.username;
if (typeof username === 'string' && username.length >= 5) {
req.customData = { valid: true, user: username.toLowerCase() };
} else {
req.customData = { valid: false };
}
next();
});Attempts:
2 left
💡 Hint
Check the username length and type, then see what customData is set to.
✗ Incorrect
The username 'User123' is a string and length is 7, which is >= 5, so customData is set with valid true and user lowercase.
🧠 Conceptual
expert2:00remaining
Which option best explains why custom validation middleware should call next() only once?
In Express, why must custom validation middleware call next() exactly once and not multiple times or skip it?
Attempts:
2 left
💡 Hint
Think about how Express middleware chains work and what happens if next() is called more than once.
✗ Incorrect
Calling next() multiple times causes Express to continue processing middleware multiple times for the same request, which can cause errors or crashes. Skipping next() stops the chain and can cause the request to hang.