0
0
Expressframework~10 mins

express-validator setup - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the express-validator check function.

Express
const { [1] } = require('express-validator');
Drag options to blanks, or click blank then click option'
Averify
Bvalidate
Ccheck
Dassert
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'validate' or 'verify' which are not exported by express-validator.
Forgetting to destructure the function from the package.
2fill in blank
medium

Complete the code to create a validation chain for the 'email' field.

Express
app.post('/signup', [ [1]('email').isEmail() ], (req, res) => { res.send('Done'); });
Drag options to blanks, or click blank then click option'
Acheck
Bassert
Cverify
Dvalidate
Attempts:
3 left
💡 Hint
Common Mistakes
Using functions not provided by express-validator like 'validate' or 'assert'.
Not passing the field name as a string.
3fill in blank
hard

Fix the error in the code to get validation results from the request.

Express
const errors = [1](req);
Drag options to blanks, or click blank then click option'
AvalidationResult
Bcheck
Cvalidate
Dassert
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'check' or 'validate' which do not extract errors.
Not calling the function with the request object.
4fill in blank
hard

Fill both blanks to import and use the validationResult function.

Express
const { [1] } = require('express-validator');

const errors = [2](req);
Drag options to blanks, or click blank then click option'
AvalidationResult
Bcheck
Dassert
Attempts:
3 left
💡 Hint
Common Mistakes
Importing 'check' instead of 'validationResult' for error extraction.
Calling a wrong function on the request object.
5fill in blank
hard

Fill all three blanks to create a validation chain for 'password' with minimum length and check errors.

Express
const { [1], [2] } = require('express-validator');

app.post('/login', [
  [1]('password').isLength({ min: 6 })
], (req, res) => {
  const errors = [2](req);
  if (!errors.isEmpty()) {
    return res.status(400).json({ errors: errors.array() });
  }
  res.send('Success');
});
Drag options to blanks, or click blank then click option'
Acheck
BvalidationResult
Dassert
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up the functions for validation and error extraction.
Not calling the validationResult function with the request.