0
0
Expressframework~5 mins

express-validator setup - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
Anpm install express
Bnpm install express-validator
Cnpm install validator
Dnpm install express-validator-cli
Which function is used to check a field in express-validator?
Acheck()
Bverify()
Cvalidate()
Dinspect()
What does validationResult(req) return?
AThe original request object
BA boolean true if valid
CAn array of validation errors
DThe response object
Where do you place validation middleware in an Express route?
AIn the app.listen callback
BAfter the route handler
CInside the route handler only
DBefore the route handler
Which of these is a valid validation rule with express-validator?
Acheck('email').isEmail()
Bcheck('name').isBoolean()
Ccheck('password').isNumber()
Dcheck('age').isEmail()
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.