typescript express validator request body

Solutions on MaxInterview for typescript express validator request body by the best coders in the world

showing results for - "typescript express validator request body"
Rania
28 Nov 2016
1// ...rest of the initial code omitted for simplicity.
2import { body, validationResult } from 'express-validator';
3
4app.post(  
5  '/user',  
6  // username must be an email  
7  body('username').isEmail(),  
8  // password must be at least 5 chars long  
9  body('password').isLength({ min: 5 }),  
10  (req: express.Request, res: express.Response) => {    
11    // Finds the validation errors in this request and wraps them in an object with handy functions    
12    const errors = validationResult(req);    
13    if (!errors.isEmpty()) {      
14      return res.status(400).json({ errors: errors.array() });    
15    }    
16    
17    User.create({      
18      username: req.body.username,      
19      password: req.body.password,    
20    }).then(user => res.json(user));  
21  },
22);
similar questions
queries leading to this page
typescript express validator request body