Guest User

Untitled

a guest
Jul 26th, 2018
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.14 KB | None | 0 0
  1. import { Request, Response } from 'express';
  2. import { BaseController } from './base-controller';
  3. import { UserService } from '../services/user.service'
  4. import { check, body, query, param, validationResult } from 'express-validator/check';
  5.  
  6. export class UserController extends BaseController {
  7.  
  8. private userService: UserService;
  9.  
  10. constructor() {
  11. super();
  12. this.userService = new UserService();
  13. }
  14.  
  15. public async createUser(req: Request, res: Response) {
  16. const viewModel = req.body;
  17.  
  18. check(viewModel.email, 'Email is not valid').isEmail();
  19. check(viewModel.password, 'Password cannot be blank').isLength({ min: 5 });
  20.  
  21. const errors = validationResult(viewModel);
  22. console.log(errors.mapped())
  23. if (!errors.isEmpty()) {
  24. return res.status(422).json({ errors: errors }); //err.mapped()
  25. }
  26. console.log(req.body)
  27. res.status(201).json(
  28. await this.userService.createUser(res, viewModel.username, viewModel.email, viewModel.password)
  29. );
  30. }
  31. }
  32.  
  33. {} <-- empty errors
  34. { username: 'testsdsdn', email: 'test.com', password: '' } <-- bad data
Add Comment
Please, Sign In to add comment