Factor out parser.validate callback

pull/977/head
Kitsune Ral 7 years ago
parent 77a9daccec
commit 33463e3907

@ -34,55 +34,43 @@ var errFn = function(err, api) {
process.exit(1); process.exit(1);
}; };
var checkOperationId = function(api) { function makeHandler(scope) {
Object.keys(api.paths).forEach(function(endpoint) { return function(err, api, metadata) {
var operationsMap = api.paths[endpoint]; if (err) {
Object.keys(operationsMap).forEach(function(verb) { console.error("%s is not valid.", scope);
if (!operationsMap[verb]["operationId"]) errFn(err, api, metadata); // Won't return
errFn("operationId is missing in " + endpoint + ", verb " + verb, api); }
})
});
return "";
};
var isDir = fs.lstatSync(opts.schema).isDirectory() Object.keys(api.paths).forEach(function (endpoint) {
var operationsMap = api.paths[endpoint];
Object.keys(operationsMap).forEach(function (verb) {
if (!operationsMap[verb]["operationId"]) {
console.log("%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) { if (isDir) {
console.log("Checking directory %s for .yaml files...", opts.schema); console.log("Checking directory %s for .yaml files...", opts.schema);
fs.readdir(opts.schema, function(err, files) { fs.readdir(opts.schema, function(err, files) {
if (err) { if (err) {
console.error(err); errFn(err); // Won't return
process.exit(1);
} }
files.forEach(function(f) { files.forEach(function(f) {
var suffix = ".yaml"; var suffix = ".yaml";
if (f.indexOf(suffix, f.length - suffix.length) > 0) { if (f.indexOf(suffix, f.length - suffix.length) > 0) {
parser.validate(path.join(opts.schema, f), function(err, api, metadata) { parser.validate(path.join(opts.schema, f), makeHandler(f));
if (!err) {
checkOperationId(api);
}
if (!err) {
console.log("%s is valid.", f);
}
else {
console.error("%s is not valid.", f);
errFn(err, api, metadata);
}
});
} }
}); });
}); });
} }
else{ else{
parser.validate(opts.schema, function(err, api) { parser.validate(opts.schema, makeHandler(opt.schema));
if (!err) { }
checkOperationId(api);
}
if (!err) {
console.log("%s is valid", opts.schema);
}
else {
errFn(err, api);
}
});
};

Loading…
Cancel
Save