Skip to content

Commit 64b1bb6

Browse files
anoadragon453richvdh
authored andcommitted
Move validator.js to scripts/ directory, update calls
1 parent 13861f7 commit 64b1bb6

File tree

4 files changed

+108
-3
lines changed

4 files changed

+108
-3
lines changed

.circleci/config.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ genmatrixassets: &genmatrixassets
3939
validateapi: &validateapi
4040
name: Validate OpenAPI specifications
4141
command: |
42-
cd api
42+
cd scripts
4343
npm install
44-
node validator.js -s "client-server"
44+
node validator.js -s "../data/api/client-server"
4545
4646
buildspeculator: &buildspeculator
4747
name: Build Speculator

scripts/package.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "swagger-cli-validator",
3+
"version": "0.0.1",
4+
"description": "",
5+
"main": "validator.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"author": "",
10+
"license": "ISC",
11+
"dependencies": {
12+
"nopt": "^3.0.2",
13+
"swagger-parser": "^3.2.1"
14+
}
15+
}

scripts/test-and-build.sh

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,16 @@ virtualenv -p python3 env
1111
python --version
1212
pip --version
1313

14+
# Install python dependencies
1415
pip install -r scripts/requirements.txt
1516

17+
# Install node dependencies
18+
npm install --prefix=scripts
19+
1620
# do sanity checks on the examples and swagger
1721
scripts/check-event-schema-examples.py
1822
scripts/check-swagger-sources.py
19-
(cd event-schemas/api && npm install && node validator.js -s "client-server")
23+
node scripts/validator.js --schema "data/api/client-server"
2024

2125
: ${GOPATH:=${WORKSPACE}/.gopath}
2226
mkdir -p "${GOPATH}"

scripts/validator.js

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
"use strict";
2+
var fs = require("fs");
3+
var nopt = require("nopt");
4+
var parser = require("swagger-parser");
5+
var path = require("path");
6+
7+
var opts = nopt({
8+
"help": Boolean,
9+
"schema": path
10+
}, {
11+
"h": "--help",
12+
"s": "--schema"
13+
});
14+
15+
if (opts.help) {
16+
console.log(
17+
"Use swagger-parser to validate against Swagger 2.0\n"+
18+
"Usage:\n"+
19+
" node validator.js -s <schema_file_or_folder>"
20+
);
21+
process.exit(0);
22+
}
23+
if (!opts.schema) {
24+
console.error("No [s]chema specified.");
25+
process.exit(1);
26+
}
27+
28+
29+
var errFn = function(err, api) {
30+
if (!err) {
31+
return;
32+
}
33+
console.error(err);
34+
process.exit(1);
35+
};
36+
37+
/**
38+
* @brief Produce a handler for parser.validate().
39+
* Recommended usage: `parser.validate(filename, makeHandler(filename));`
40+
* or `parser.validate(schema, makeHandler());`.
41+
* @param scope - usually a filename, this will be used to denote
42+
* an (in)valid schema in console output; "Schema" if undefined
43+
* @returns {function} the handler that can be passed to parser.validate
44+
*/
45+
function makeHandler(scope) {
46+
if (!scope)
47+
scope = "Schema";
48+
return function(err, api, metadata) {
49+
if (err) {
50+
console.error("%s is not valid.", scope || "Schema");
51+
errFn(err, api, metadata); // Won't return
52+
}
53+
54+
Object.keys(api.paths).forEach(function (endpoint) {
55+
var operationsMap = api.paths[endpoint];
56+
Object.keys(operationsMap).forEach(function (verb) {
57+
if (!operationsMap[verb]["operationId"]) {
58+
console.error("%s is not valid", scope);
59+
errFn("operationId is missing in " + endpoint + ", verb " + verb, api);
60+
}
61+
})
62+
});
63+
64+
console.log("%s is valid.", scope);
65+
}
66+
}
67+
68+
var isDir = fs.lstatSync(opts.schema).isDirectory();
69+
if (isDir) {
70+
console.log("Checking directory %s for .yaml files...", opts.schema);
71+
fs.readdir(opts.schema, function(err, files) {
72+
if (err) {
73+
errFn(err); // Won't return
74+
}
75+
files.forEach(function(f) {
76+
var suffix = ".yaml";
77+
if (f.indexOf(suffix, f.length - suffix.length) > 0) {
78+
parser.validate(path.join(opts.schema, f), makeHandler(f));
79+
}
80+
});
81+
});
82+
}
83+
else{
84+
parser.validate(opts.schema, makeHandler(opts.schema));
85+
}
86+

0 commit comments

Comments
 (0)