-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #45 from aspear/aspear/add-json-output
feat: Added -j/--json output option to cli-validator
- Loading branch information
Showing
4 changed files
with
93 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
const each = require('lodash/each'); | ||
|
||
// get line-number-producing, 'magic' code from Swagger Editor | ||
const getLineNumberForPath = require(__dirname + '/../../plugins/ast/ast') | ||
.getLineNumberForPath; | ||
|
||
// function to print the results as json to the console. | ||
module.exports = function printJson(results, originalFile) { | ||
const types = ['errors', 'warnings']; | ||
types.forEach(type => { | ||
each(results[type], problems => { | ||
problems.forEach(problem => { | ||
// TODO figure out how to include the config option that caused the error/warning | ||
// and inject that as additional data. | ||
|
||
let path = problem.path; | ||
|
||
// path needs to be an array to get the line number | ||
if (!Array.isArray(path)) { | ||
path = path.split('.'); | ||
} | ||
|
||
// get line number from the path of strings to the problem | ||
// as they say in src/plugins/validation/semantic-validators/hook.js, | ||
// | ||
// "it's magic!" | ||
// | ||
const line = getLineNumberForPath(originalFile, path); | ||
|
||
// add the line number to the result JSON | ||
problem.line = line; | ||
}); | ||
}); | ||
}); | ||
// render the results to json in the console with 2 char spacing | ||
const jsonstr = JSON.stringify(results, null, 2); | ||
console.log(jsonstr); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters