From 3c66e149f3628b211a4d07cc58874aabfb99a324 Mon Sep 17 00:00:00 2001 From: Andrew Morgan Date: Fri, 5 Feb 2021 12:40:45 +0000 Subject: [PATCH] Move validator.js to scripts/ directory, update calls --- .circleci/config.yml | 4 +- scripts/package.json | 15 +++++++ scripts/test-and-build.sh | 6 ++- scripts/validator.js | 86 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 108 insertions(+), 3 deletions(-) create mode 100644 scripts/package.json create mode 100644 scripts/validator.js diff --git a/.circleci/config.yml b/.circleci/config.yml index 274dc007..e3ac14d8 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -39,9 +39,9 @@ genmatrixassets: &genmatrixassets validateapi: &validateapi name: Validate OpenAPI specifications command: | - cd api + cd scripts npm install - node validator.js -s "client-server" + node validator.js -s "../data/api/client-server" buildspeculator: &buildspeculator name: Build Speculator diff --git a/scripts/package.json b/scripts/package.json new file mode 100644 index 00000000..84b9dd7b --- /dev/null +++ b/scripts/package.json @@ -0,0 +1,15 @@ +{ + "name": "swagger-cli-validator", + "version": "0.0.1", + "description": "", + "main": "validator.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "", + "license": "ISC", + "dependencies": { + "nopt": "^3.0.2", + "swagger-parser": "^3.2.1" + } +} diff --git a/scripts/test-and-build.sh b/scripts/test-and-build.sh index f4c3a90f..76f49e2e 100755 --- a/scripts/test-and-build.sh +++ b/scripts/test-and-build.sh @@ -11,12 +11,16 @@ virtualenv -p python3 env python --version pip --version +# Install python dependencies pip install -r scripts/requirements.txt +# Install node dependencies +npm install --prefix=scripts + # do sanity checks on the examples and swagger scripts/check-event-schema-examples.py scripts/check-swagger-sources.py -(cd event-schemas/api && npm install && node validator.js -s "client-server") +node scripts/validator.js --schema "data/api/client-server" : ${GOPATH:=${WORKSPACE}/.gopath} mkdir -p "${GOPATH}" diff --git a/scripts/validator.js b/scripts/validator.js new file mode 100644 index 00000000..c20dd993 --- /dev/null +++ b/scripts/validator.js @@ -0,0 +1,86 @@ +"use strict"; +var fs = require("fs"); +var nopt = require("nopt"); +var parser = require("swagger-parser"); +var path = require("path"); + +var opts = nopt({ + "help": Boolean, + "schema": path +}, { + "h": "--help", + "s": "--schema" +}); + +if (opts.help) { + console.log( + "Use swagger-parser to validate against Swagger 2.0\n"+ + "Usage:\n"+ + " node validator.js -s " + ); + process.exit(0); +} +if (!opts.schema) { + console.error("No [s]chema specified."); + process.exit(1); +} + + +var errFn = function(err, api) { + if (!err) { + return; + } + console.error(err); + process.exit(1); +}; + +/** + * @brief Produce a handler for parser.validate(). + * Recommended usage: `parser.validate(filename, makeHandler(filename));` + * or `parser.validate(schema, makeHandler());`. + * @param scope - usually a filename, this will be used to denote + * an (in)valid schema in console output; "Schema" if undefined + * @returns {function} the handler that can be passed to parser.validate + */ +function makeHandler(scope) { + if (!scope) + scope = "Schema"; + return function(err, api, metadata) { + if (err) { + console.error("%s is not valid.", scope || "Schema"); + errFn(err, api, metadata); // Won't return + } + + Object.keys(api.paths).forEach(function (endpoint) { + var operationsMap = api.paths[endpoint]; + Object.keys(operationsMap).forEach(function (verb) { + if (!operationsMap[verb]["operationId"]) { + console.error("%s is not valid", scope); + errFn("operationId is missing in " + endpoint + ", verb " + verb, api); + } + }) + }); + + console.log("%s is valid.", scope); + } +} + +var isDir = fs.lstatSync(opts.schema).isDirectory(); +if (isDir) { + console.log("Checking directory %s for .yaml files...", opts.schema); + fs.readdir(opts.schema, function(err, files) { + if (err) { + errFn(err); // Won't return + } + files.forEach(function(f) { + var suffix = ".yaml"; + if (f.indexOf(suffix, f.length - suffix.length) > 0) { + parser.validate(path.join(opts.schema, f), makeHandler(f)); + } + }); + }); +} +else{ + parser.validate(opts.schema, makeHandler(opts.schema)); +} +