aboutsummaryrefslogblamecommitdiffhomepage
path: root/server/middlewares/validators/utils.ts
blob: 8845f83995be3536abd9c5f3f1b05be5b6f0e3bf (plain) (tree)
1
2
3
4
5
6
7
8
                                                          
                                  
 
                                      
 
                                                                                                                  
                                      
 


                                                                                                

   



                                                                              
 


             
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, statusCode = 400) {
  const errors = validationResult(req)

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

  return next()
}

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

export {
  checkErrors
}