Skip to content

Commit

Permalink
Validate swagger files
Browse files Browse the repository at this point in the history
  • Loading branch information
gkoberger committed Oct 4, 2016
1 parent 9c2a1a5 commit 4bf6f73
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 8 deletions.
6 changes: 0 additions & 6 deletions api.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,6 @@ exports.api = function(args, opts) {
return;
}

if(!file) {
console.log("We couldn't find a Swagger file.".red);
console.log("Run " + "api init".yellow + " to get started.");
process.exit();
}

var apiId = crypto.randomBytes(15).toString('hex');

if(!swagger['x-api-id']) {
Expand Down
12 changes: 12 additions & 0 deletions lib/validate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
var figures = require('figures');

exports.swagger = true;
exports.login = false;
exports.desc = "Validate your Swagger file";
exports.category = "utility";

exports.run = function(config, info) {
console.log(figures.tick.green + " Success!".green + " Valid Swagger file");
process.exit();
};

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"dependencies": {
"base-store": "^0.4.4",
"colors": "^1.1.2",
"figures": "^1.7.0",
"git-utils": "^4.1.2",
"glob": "^7.1.0",
"inquirer": "^1.2.1",
Expand Down
31 changes: 29 additions & 2 deletions utils.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
var fs = require('fs');
var os = require('os');
var path = require('path');
var glob = require("glob")
var glob = require('glob')
var figures = require('figures');

var _ = require('lodash');
var git = require('git-utils');
Expand All @@ -28,7 +29,33 @@ exports.findSwagger = function(cb, opts) {
metadata: true,
}).then((generatedSwagger) => {
generatedSwagger = JSON.parse(generatedSwagger);
cb(undefined, generatedSwagger, generatedSwagger['x-si-base']); // TODO! We need to fix the file!

if(!generatedSwagger['x-si-base']) {
console.log("We couldn't find a Swagger file.".red);
console.log("Run " + "api init".yellow + " to get started.");
process.exit();
}

swagger.validate(generatedSwagger, function(err, api) {
if(err) {
console.log("");
console.log("Error validating Swagger!".red);
console.log("");
if(err.details) {
_.each(err.details, function(detail) {
var at = detail.path && detail.path.length ? " (at " + detail.path.join('.') + ")" : "";
console.log(" " + figures.cross.red + " " + detail.message + at.grey);
});
} else {
console.log(figures.cross.red + " " + err.message);
}
console.log("");
process.exit();
return;
}

cb(undefined, generatedSwagger, generatedSwagger['x-si-base']);
});
});

};
Expand Down

0 comments on commit 4bf6f73

Please sign in to comment.