Factor out parser.validate callback

pull/1017/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) {
return function(err, api, metadata) {
if (err) {
console.error("%s is not valid.", scope);
errFn(err, api, metadata); // Won't return
}
Object.keys(api.paths).forEach(function (endpoint) { Object.keys(api.paths).forEach(function (endpoint) {
var operationsMap = api.paths[endpoint]; var operationsMap = api.paths[endpoint];
Object.keys(operationsMap).forEach(function (verb) { Object.keys(operationsMap).forEach(function (verb) {
if (!operationsMap[verb]["operationId"]) if (!operationsMap[verb]["operationId"]) {
console.log("%s is not valid", scope);
errFn("operationId is missing in " + endpoint + ", verb " + verb, api); errFn("operationId is missing in " + endpoint + ", verb " + verb, api);
}
}) })
}); });
return "";
};
var isDir = fs.lstatSync(opts.schema).isDirectory() 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