UNPKG

express-openapi-validate

Version:

Express middleware to validate request based on an OpenAPI 3 document

80 lines (77 loc) 3.29 kB
"use strict"; /* Copyright 2018 Santeri Hiltunen Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.oasPathToExpressPath = exports.mapOasSchemaToJsonSchema = exports.walkSchema = exports.resolveReference = void 0; const lodash_1 = __importDefault(require("lodash")); const isReferenceObject = (x) => x.$ref !== undefined; const resolveReference = (document, object) => { if (isReferenceObject(object)) { if (!object.$ref.startsWith("#/components/")) { throw new Error(`Unsupported $ref=${object.$ref}`); } const path = lodash_1.default.drop(object.$ref.split("/"), 1); const resolvedObject = lodash_1.default.get(document, path); if (resolvedObject === undefined) { throw new Error(`Object not found with $ref=${object.$ref}`); } return resolvedObject; } return object; }; exports.resolveReference = resolveReference; const arrayFields = ["allOf", "anyOf", "oneOf"]; const schemaFields = ["items", "not", "additionalProperties"]; const walkSchema = (originalSchema, mapper) => { let schema = mapper(originalSchema); const walk = (s) => exports.walkSchema(s, mapper); if (schema.properties !== undefined) { schema = { ...schema, properties: lodash_1.default.mapValues(schema.properties, walk) }; } arrayFields .filter((f) => f in schema) .forEach((f) => { schema = { ...schema, [f]: schema[f].map(walk) }; }); schemaFields .filter((f) => f in schema) .forEach((f) => { const nestedSchema = schema[f]; if (f === "additionalProperties" && typeof nestedSchema === "boolean") { return; } schema = { ...schema, [f]: walk(nestedSchema) }; }); return schema; }; exports.walkSchema = walkSchema; const mapOasSchemaToJsonSchema = (originalSchema, document) => { const mapOasFieldsToJsonSchemaFields = (s) => { const schema = exports.resolveReference(document, s); if (Array.isArray(schema.type)) { throw new TypeError("Type field in schema must not be an array"); } if (Array.isArray(schema.items)) { throw new TypeError("Items field in schema must not be an array"); } return schema; }; return exports.walkSchema(originalSchema, mapOasFieldsToJsonSchemaFields); }; exports.mapOasSchemaToJsonSchema = mapOasSchemaToJsonSchema; const oasPathToExpressPath = (path) => path.replace(/\{([^}]+)\}/g, ":$1"); exports.oasPathToExpressPath = oasPathToExpressPath; //# sourceMappingURL=schema-utils.js.map