Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { Request, Response } from 'express';
- import { BaseController } from './base-controller';
- import { UserService } from '../services/user.service'
- import { check, body, query, param, validationResult } from 'express-validator/check';
- export class UserController extends BaseController {
- private userService: UserService;
- constructor() {
- super();
- this.userService = new UserService();
- }
- public async createUser(req: Request, res: Response) {
- const viewModel = req.body;
- check(viewModel.email, 'Email is not valid').isEmail();
- check(viewModel.password, 'Password cannot be blank').isLength({ min: 5 });
- const errors = validationResult(viewModel);
- console.log(errors.mapped())
- if (!errors.isEmpty()) {
- return res.status(422).json({ errors: errors }); //err.mapped()
- }
- console.log(req.body)
- res.status(201).json(
- await this.userService.createUser(res, viewModel.username, viewModel.email, viewModel.password)
- );
- }
- }
- {} <-- empty errors
- { username: 'testsdsdn', email: 'test.com', password: '' } <-- bad data
Add Comment
Please, Sign In to add comment