Challenge - 5 Problems
Express Validator 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-validator middleware?
Consider this Express route using express-validator. What will be the response if the request body is { "email": "invalidemail", "age": 17 }?
Express
import express from 'express'; import { body, validationResult } from 'express-validator'; const app = express(); app.use(express.json()); app.post('/register', [ body('email').isEmail(), body('age').isInt({ min: 18 }) ], (req, res) => { const errors = validationResult(req); if (!errors.isEmpty()) { return res.status(400).json({ errors: errors.array() }); } res.json({ message: 'User registered' }); });
Attempts:
2 left
💡 Hint
Check which validations fail for the given input values.
✗ Incorrect
The email is invalid and age is less than 18, so both validations fail. The response returns status 400 with both errors.
📝 Syntax
intermediate1:30remaining
Which option correctly imports and uses express-validator in an Express app?
Select the option that correctly sets up express-validator middleware for validating a username field as non-empty.
Attempts:
2 left
💡 Hint
Check the official express-validator import and method names.
✗ Incorrect
Option A correctly imports 'body' from express-validator and uses it to validate the 'username' field.
🔧 Debug
advanced2:00remaining
Why does this express-validator code not catch validation errors?
Given this code snippet, why does the server always respond with 'User created' even if the input is invalid?
app.post('/create', [body('email').isEmail()], (req, res) => {
res.send('User created');
});
Attempts:
2 left
💡 Hint
Think about how express-validator reports validation errors.
✗ Incorrect
express-validator requires calling validationResult(req) to check for errors. Without this, errors are ignored.
❓ state_output
advanced2:00remaining
What is the value of errors after this validation?
Given this code snippet, what will be the value of errors.array() if the request body is { "password": "123" }?
app.post('/signup', [
body('password').isLength({ min: 6 })
], (req, res) => {
const errors = validationResult(req);
res.json({ errors: errors.array() });
});
Attempts:
2 left
💡 Hint
Check the default error message for isLength validator.
✗ Incorrect
The default error message for isLength is 'Invalid value' when the length condition fails.
🧠 Conceptual
expert2:30remaining
Which statement about express-validator setup is true?
Choose the correct statement about how express-validator integrates with Express routes.
Attempts:
2 left
💡 Hint
Think about how validation errors are handled in express-validator.
✗ Incorrect
express-validator requires explicit call to validationResult(req) inside route handler to check errors. It does not send responses automatically.