"use strict"; // const Joi = require("@hapi/joi"); const errors = require("../errors"); const {validate, ValidationError} = require("../validator-lib"); module.exports = function createPayloadValidator(validator) { return function validatePayload(req, _res, next) { try { validate(validator, req.body); } catch (error) { if (error instanceof ValidationError) { console.log(require("util").inspect(error, {colors: true, depth: null})); let errorMessage = (error.path != null) ? `(property: ${error.path.join(".")}) ${error.message}` : error.message; throw new errors.MissingParameter(`Validation error: ${errorMessage}`); } else { throw error; } } next(); // let result = Joi.validate(req.body, schema); // if (result.error != null) { // /* FIXME: Actually produce meaningful output based on the Joi output */ // console.log(require("util").inspect(result.error, {colors: true, depth: null})); // throw new errors.MissingParameter("Something in the validation was wrong"); // } else { // next(); // } } };