UNPKG

@egodigital/egoose

Version:

Helper classes and functions for Node.js 10 or later.

121 lines 4.18 kB
"use strict"; /** * This file is part of the @egodigital/egoose distribution. * Copyright (c) e.GO Digital GmbH, Aachen, Germany (https://www.e-go-digital.com/) * * @egodigital/egoose is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation, version 3. * * @egodigital/egoose is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ Object.defineProperty(exports, "__esModule", { value: true }); const _ = require("lodash"); const express = require("express"); const index_1 = require("../index"); /** * Creates Express middlewares for validating JSON input. * * @param {JsonObjectOptions|joi.ObjectSchema} [optsOrSchema] Custom options or schema. * * @return {express.RequestHandler[]} The created handler(s). */ function jsonObject(optsOrSchema) { let opts; if (optsOrSchema) { if (optsOrSchema['isJoi']) { opts = { schema: optsOrSchema, }; } else { opts = optsOrSchema; } } if (!opts) { opts = {}; } let failedHandler = opts.failedHandler; if (!failedHandler) { // default failedHandler = (ctx) => { const RESULT = { success: false, data: { details: ctx.details, reason: ctx.reason, }, }; return ctx.response .status(400) .header('content-type', 'application/json; charset=utf-8') .send(Buffer.from(JSON.stringify(RESULT), 'utf8')); }; } const HANDLERS = []; HANDLERS.push(express.json(opts.options)); if (opts.schema) { HANDLERS.push(async function (req, res, next) { let reason = 'no_object'; let details; const BODY = req.body; if (_.isNull(BODY) && index_1.toBooleanSafe(opts.canBeNull)) { return next(); // can be (null) } if (_.isUndefined(BODY) && index_1.toBooleanSafe(opts.canBeUndefined)) { return next(); // can be (undefined) } if (_.isObjectLike(BODY)) { reason = 'invalid_structure'; const JSON_VALIDATION = opts.schema.validate(BODY); if (_.isNil(JSON_VALIDATION.error)) { return next(); } else { details = JSON_VALIDATION.error.message; } } return await Promise.resolve(failedHandler({ body: BODY, details: details, reason: reason, request: req, response: res, })); }); } else { // check if JSON only HANDLERS.push(async function (req, res, next) { let reason = 'no_object'; let details; const BODY = req.body; if (_.isNull(BODY) && index_1.toBooleanSafe(opts.canBeNull)) { return next(); // can be (null) } if (_.isUndefined(BODY) && index_1.toBooleanSafe(opts.canBeUndefined)) { return next(); // can be (undefined) } if (_.isObjectLike(BODY)) { return next(); } details = `Request body is of type '${_.isNull(BODY) ? 'null' : (typeof BODY)}'`; return await Promise.resolve(failedHandler({ body: BODY, details: details, reason: reason, request: req, response: res, })); }); } return HANDLERS; } exports.jsonObject = jsonObject; //# sourceMappingURL=validation.js.map