Files
zulip/tools/check-swagger
Yago González 33e4855306 lint: Ban non-unique operationIds in Swagger.
The Swagger specification indicates that all operationId values should be
unique. However, SwaggerParser doesn't complain during validation if that
doesn't happen, so this commit adds our own method to identify these
cases.

Also, the violations of this rule have been fixed.
2017-06-26 22:02:26 -04:00

50 lines
1.7 KiB
Plaintext

var SwaggerParser = require('swagger-parser'),
_ = require('underscore');
function check_duplicate_operationids(api) {
var operation_ids = [];
_.each(api.paths, function(endpoint) {
_.each(endpoint, function(value) {
var operation_id = value.operationId;
if(operation_id !== undefined) {
if(operation_ids.indexOf(operation_id) >= 0) {
console.error('In', file + ':');
console.error('Duplicate operationId:', operation_id);
process.exitCode = 1;
} else {
operation_ids.push(operation_id);
}
}
});
});
}
function validate_swagger(file) {
SwaggerParser.validate(file)
.then(function(api) {
// Let's make sure that there aren't any duplicate operationids,
// until this issue is fixed:
// https://github.com/BigstickCarpet/swagger-parser/issues/68
check_duplicate_operationids(api);
return;
})
.catch(function(err) {
// There is something wrong. Display the validation errors
console.error('In', file + ':');
console.error(err.message);
process.exitCode = 1;
});
}
// Iterate through the changed files, passed in the arguments.
// The two first arguments are the call to the Node interpreter and this
// script, hence the starting point at 2.
for(var i = 2; i < process.argv.length; i++) {
var file = process.argv[i];
// Run the validator only for YAML files inside static/swagger
if(file.startsWith('static/swagger')) {
validate_swagger(file);
}
}