js request validator

Solutions on MaxInterview for js request validator by the best coders in the world

showing results for - "js request validator"
Leonardo
13 Nov 2018
1// ...rest of the initial code omitted for simplicity.const { body, validationResult } = require('express-validator');
2app.post(  '/user',
3         // username must be an email  
4        body('username').isEmail(),  // password must be at least 5 chars long 
5body('password').isLength({ min: 5 }),  (req, res) => {
6  // Finds the validation errors in this request and wraps them in an object with handy functions    
7const errors = validationResult(req);    if (!errors.isEmpty()) {
8  return res.status(400)
9  .json({ errors: errors.array() });   
10}    
11  User.create({ 
12    username: req.body.username,
13    password: req.body.password, 
14  })
15  .then(user => res.json(user));  },);