-
Notifications
You must be signed in to change notification settings - Fork 382
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from matrix-org/anoa/support-rendered-data
Reinstate and fix schema validation files
- Loading branch information
Showing
6 changed files
with
167 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <schema_file_or_folder>" | ||
); | ||
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)); | ||
} | ||
|