"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.DefaultRequestValidator = exports.ParamsValidator = exports.QueryValidator = exports.BodyValidator = exports.RequestValidator = void 0; const Validator = require("fastest-validator"); const getValidator = (options) => { const o = options !== null && typeof options === 'object' ? options : {}; return new Validator({ useNewCustomCheckerFunction: true, ...o }); }; const RequestValidator = (schemas, errorHandler, options) => { const v = getValidator(options); const checkFuncs = []; for (const key in schemas) { if (Boolean(schemas[key]) === true) { checkFuncs.push({ key, checkFunc: v.compile(schemas[key]), }); } } return async (req, res, next) => { try { const errors = {}; for (const { key, checkFunc } of checkFuncs) { const result = await checkFunc(req[key]); if (result !== true) { errors[key] = result; } } if (Object.keys(errors).length > 0) { if (typeof errorHandler === 'function') { errorHandler(errors, req, res, next); } else { next(errors); } return; } next(); } catch (err) { next(err); } }; }; exports.RequestValidator = RequestValidator; const defaultRequestValidatorHandler = (mayBeErrors, _req, res, next) => { const errors = typeof mayBeErrors === 'object' && Array.isArray(mayBeErrors) === false && mayBeErrors !== null ? mayBeErrors : {}; if (Object.keys(errors).length === 0) { next(); return; } const paramsValidationErrors = errors?.params; if (Array.isArray(paramsValidationErrors) && paramsValidationErrors.length > 0) { res.sendStatus(404); return; } if ((Array.isArray(errors?.query) && errors.query.length > 0) || (Array.isArray(errors?.body) && errors.body.length > 0)) { res.status(422).send(errors); return; } next(mayBeErrors); }; const BodyValidator = (schema, errorHandler, options) => { const v = getValidator(options); const checkFunc = v.compile(schema); return async (req, res, next) => { try { const result = await checkFunc(req.body); if (result !== true) { const errors = { body: result, }; if (typeof errorHandler === 'function') { errorHandler(errors, req, res, next); } else { next(errors); } return; } next(); } catch (err) { next(err); } }; }; exports.BodyValidator = BodyValidator; const QueryValidator = (schema, errorHandler, options) => { const v = getValidator(options); const checkFunc = v.compile(schema); return async (req, res, next) => { try { const result = await checkFunc(req.query); if (result !== true) { const errors = { query: result, }; if (typeof errorHandler === 'function') { errorHandler(errors, req, res, next); } else { next(errors); } return; } next(); } catch (err) { next(err); } }; }; exports.QueryValidator = QueryValidator; const ParamsValidator = (schema, errorHandler, options) => { const v = getValidator(options); const checkFunc = v.compile(schema); return async (req, res, next) => { try { const result = await checkFunc(req.params); if (result !== true) { const errors = { params: result, }; if (typeof errorHandler === 'function') { errorHandler(errors, req, res, next); } else { next(errors); } return; } next(); } catch (err) { next(err); } }; }; exports.ParamsValidator = ParamsValidator; const DefaultRequestValidator = (schemas, options) => { return RequestValidator(schemas, defaultRequestValidatorHandler, options); }; exports.DefaultRequestValidator = DefaultRequestValidator;