import { validationResult } from "express-validator";

import { ApiError } from "../utils/ApiError.js";

export const handleValidation = (req, res, next) => {
  const result = validationResult(req);

  if (result.isEmpty()) {
    return next();
  }

  const message = result
    .array()
    .map((item) => item.msg)
    .filter(Boolean)
    .join(" ");

  return next(new ApiError(400, message || "Paramètres invalides."));
};
