Recall & Review
beginner
What is
express-validator used for in an Express app?express-validator helps check and validate user input in Express apps to keep data clean and safe.
Click to reveal answer
beginner
How do you install
express-validator in your project?Run npm install express-validator in your project folder to add it.
Click to reveal answer
beginner
Which function from
express-validator do you use to define validation rules?You use check() or body() to set rules for request data fields.
Click to reveal answer
intermediate
What does
validationResult(req) do in your route handler?It collects the results of validations and tells you if there are errors in the user input.
Click to reveal answer
intermediate
Show a simple example of setting up
express-validator in an Express POST route.<pre>import { check, validationResult } from 'express-validator';
app.post('/signup', [
check('email').isEmail(),
check('password').isLength({ min: 6 })
], (req, res) => {
const errors = validationResult(req);
if (!errors.isEmpty()) {
return res.status(400).json({ errors: errors.array() });
}
res.send('Signup successful');
});</pre>Click to reveal answer
What command installs
express-validator?✗ Incorrect
The correct package is express-validator, installed with npm install express-validator.
Which function is used to check a field in
express-validator?✗ Incorrect
check() is used to define validation rules for request fields.
What does
validationResult(req) return?✗ Incorrect
It returns an object containing any validation errors found in the request.
Where do you place validation middleware in an Express route?
✗ Incorrect
Validation middleware runs before the route handler to check input first.
Which of these is a valid validation rule with
express-validator?✗ Incorrect
check('email').isEmail() correctly checks if the email field is a valid email.
Explain how to set up
express-validator in an Express POST route to validate user input.Think about middleware placement and error checking.
You got /5 concepts.
Describe the role of
validationResult in the validation process with express-validator.It helps decide if input is good or not.
You got /4 concepts.