gb_utils
Version:
All utils file for beta
33 lines (29 loc) • 717 B
JavaScript
// Utils Import
const sendResponse = require("./responseUtils");
const logger = require("./loggerUtils");
const validateUtil = {};
validateUtil.validateBody = (schema) => async (req, res, next) => {
const options = {
abortEarly: false, // include all errors
allowUnknown: true, // ignore unknown props
stripUnknown: true, // remove unknown props
};
const { error, value } = schema.validate(req.body, options);
if (error) {
logger.error(
JSON.stringify(
error.details.map((each) => each.message),
null,
2
)
);
await sendResponse.validationError(
res,
error.details.map((each) => each.message)
);
} else {
req.body = value;
next();
}
};
module.exports = validateUtil;