UNPKG

@mojaloop/ml-schema-transformer-lib

Version:
93 lines (92 loc) 3.98 kB
"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/lib/validation/validator.ts var validator_exports = {}; __export(validator_exports, { Validator: () => Validator }); module.exports = __toCommonJS(validator_exports); var import_swagger_parser = __toESM(require("@apidevtools/swagger-parser")); var import_openapi_backend = require("openapi-backend"); var import_fast_safe_stringify = __toESM(require("fast-safe-stringify")); var Validator = class { apiDoc; apiSpec; apiValidator; ajvOpts = { allErrors: true, coerceTypes: true, strict: false }; /** * @param apiDoc - The path to the OpenAPI document or the document itself */ constructor(apiDoc) { this.apiDoc = apiDoc; this.apiSpec = {}; this.apiValidator = {}; } async initialize() { this.apiSpec = await import_swagger_parser.default.validate(this.apiDoc); this.apiValidator = new import_openapi_backend.OpenAPIValidator({ definition: this.apiSpec, ajvOpts: this.ajvOpts }); } validateRequest(request, returnErrors = false) { const validation = this.apiValidator.validateRequest(request); if (validation.errors && validation.errors.length > 0) { if (returnErrors) return validation.errors; throw new Error(`Validation errors: ${(0, import_fast_safe_stringify.default)(validation.errors)}`); } return true; } validateBody(partialRequest, returnErrors = false) { const { body, path, method } = partialRequest; const validation = this.apiValidator.validateRequest({ path, method, body, headers: {} }); if (validation.errors) { const bodyErrors = validation.errors.filter((error) => error.instancePath === "/requestBody"); if (bodyErrors && bodyErrors.length > 0) { if (returnErrors) return bodyErrors; throw new Error(`Validation errors: ${(0, import_fast_safe_stringify.default)(bodyErrors)}`); } } return true; } validateHeaders(partialRequest, returnErrors = false) { const { headers, path, method } = partialRequest; const validation = this.apiValidator.validateRequest({ path, method, headers, body: {} }); if (validation.errors) { const headersErrors = validation.errors.filter((error) => error.instancePath === "/header"); if (headersErrors && headersErrors.length > 0) { if (returnErrors) return headersErrors; throw new Error(`Validation errors: ${(0, import_fast_safe_stringify.default)(headersErrors)}`); } } return true; } }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { Validator }); //# sourceMappingURL=validator.js.map