zulip/tools/check-openapi

50 lines
1.6 KiB
JavaScript
Executable File

#!/usr/bin/env node
const SwaggerParser = require('swagger-parser');
function check_duplicate_operationids(file, api) {
const operation_ids = [];
for (const endpoint of api.paths) {
for (const value of endpoint) {
const operation_id = value.operationId;
if (operation_id !== undefined) {
if (operation_ids.includes(operation_id)) {
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(file, 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 (const file of process.argv.slice(2)) {
// Run the validator only for YAML files inside static/swagger
if (file.startsWith('static/swagger')) {
validate_swagger(file);
}
}