aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/middlewares/validators/utils.ts
blob: ea107bbe8f67b5036b7f24a964d470de76b7d968 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import { validationResult } from 'express-validator/check'
import * as express from 'express'

import { logger } from '../../helpers'

function checkErrors (req: express.Request, res: express.Response, next: express.NextFunction) {
  const errors = validationResult(req)

  if (!errors.isEmpty()) {
    logger.warn('Incorrect request parameters', { path: req.originalUrl, err: errors.mapped() })
    return res.status(400).json({ errors: errors.mapped() })
  }

  return next()
}

// ---------------------------------------------------------------------------

export {
  checkErrors
}