UNPKG

@adpt/cloud

Version:
64 lines 2.37 kB
"use strict"; /* * Copyright 2018-2019 Unbounded Systems, LLC * * 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. */ Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); const debug_1 = tslib_1.__importDefault(require("debug")); const jsonStableStringify = require("json-stable-stringify"); const jsonschema_1 = require("jsonschema"); const crypto_1 = require("crypto"); const jsonSchemaSchema = require("./json-schema-4.json"); const swagger2schema = require("./swagger2schema.json"); const debug = debug_1.default("adapt:cloud:swagger2gql"); const knownSwaggerSpecs = [ "3f9280af09efeaf35940feb34b7449de", "7cf922b28a4f6562069fe68c716bb6d7", "1cf94afa6c91f4c71cb4c37182ecee6e", "5eb0190ff7afd4ef33bc9dec64a8ed04", ]; let validator; const validSet = new Set(knownSwaggerSpecs); function validSetKey(cand) { const json = jsonStableStringify(cand); return crypto_1.createHash("md5").update(json, "utf8").digest("hex"); } function isValidated(cand) { const key = validSetKey(cand); const found = validSet.has(key); if (!found) debug(`Swagger spec with hash ${key} has not been pre-validated`); return found; } function validated(cand) { const key = validSetKey(cand); validSet.add(key); } function validateSwagger2(cand) { if (isValidated(cand)) return cand; if (!validator) { validator = new jsonschema_1.Validator(); validator.addSchema(jsonSchemaSchema, "http://json-schema.org/draft-04/schema"); } const result = validator.validate(cand, swagger2schema); if (result.errors.length > 0) { throw new Error("Invalid swagger specification: " + result.errors.join("\n")); } validated(cand); return result.instance; } exports.validateSwagger2 = validateSwagger2; //# sourceMappingURL=swagger_validate.js.map