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

                                                                                                
                                                            

   


               












                                                                                                
                                                                              
 
        

                     
 
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()
}

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

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

    return true
  }

  return false
}

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

export {
  checkErrors,
  areValidationErrors
}