const { validationResult } = require('express-validator');
const { deleteUploadedFiles } = require('../util/fileHandler');

module.exports = (req, res, next) => {
  const errors = validationResult(req);
  if (!errors.isEmpty()) {
    const error = new Error('Invalid request!');
    error.statusCode = 422;
    error.data = errors.array();

    deleteUploadedFiles(req);

    throw error;
  }
  next();
};
