diff --git a/README.md b/README.md index dd8208963..d5a968b2a 100644 --- a/README.md +++ b/README.md @@ -62,6 +62,7 @@ To build these, run `npm run pkg` in the root. The binaries (lint-openapi-linux #### [options] - -s (--report_statistics) : Print a simple report at the end of the output showing the frequency, in percentage, of each error/warning. +- -e (--errors_only) : Only print the errors, ignore the warnings. - -d (--default_mode) : This option turns off [configuration](#configuration) and runs the validator in [default mode](#default-mode). - -p (--print_validator_modules) : Print the name of the validator source file the error/warning was caught it. This can be helpful for developing validations. - -n (--no_colors) : The output is colored by default. If this bothers you, this flag will turn off the coloring. diff --git a/src/cli-validator/index.js b/src/cli-validator/index.js index b93cf4443..2516b9c90 100755 --- a/src/cli-validator/index.js +++ b/src/cli-validator/index.js @@ -38,6 +38,10 @@ program ) .option( '-c, --config ', 'path to config file, used instead of .validaterc if provided' + ) + .option( + '-e, --errors_only', + 'only print the errors, ignore the warnings' ); /* prettier-ignore */ diff --git a/src/cli-validator/runValidator.js b/src/cli-validator/runValidator.js index d33a9a512..d96e3f8c3 100644 --- a/src/cli-validator/runValidator.js +++ b/src/cli-validator/runValidator.js @@ -37,6 +37,7 @@ const processInput = async function(program) { const turnOffColoring = !!program.no_colors; const defaultMode = !!program.default_mode; const jsonOutput = !!program.json; + const errorsOnly = !!program.errors_only; const configFileOverride = program.config; @@ -231,11 +232,24 @@ const processInput = async function(program) { continue; } + // the warning property tells the user if warnings are included as part of the output + // if errorsOnly is true, only errors will be returned, so need to force this to false + if (errorsOnly) { + results.warning = false; + } + if (jsonOutput) { - printJson(results, originalFile); + printJson(results, originalFile, errorsOnly); } else { if (results.error || results.warning) { - print(results, chalk, printValidators, reportingStats, originalFile); + print( + results, + chalk, + printValidators, + reportingStats, + originalFile, + errorsOnly + ); // fail on errors, but not if there are only warnings if (results.error) exitCode = 1; } else { diff --git a/src/cli-validator/utils/printJsonResults.js b/src/cli-validator/utils/printJsonResults.js index dc5ee4692..d847a40f5 100644 --- a/src/cli-validator/utils/printJsonResults.js +++ b/src/cli-validator/utils/printJsonResults.js @@ -5,8 +5,8 @@ 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']; +module.exports = function printJson(results, originalFile, errorsOnly) { + const types = errorsOnly ? ['errors'] : ['errors', 'warnings']; types.forEach(type => { each(results[type], problems => { problems.forEach(problem => { @@ -32,6 +32,9 @@ module.exports = function printJson(results, originalFile) { }); }); }); + if (errorsOnly) { + delete results.warnings; + } // render the results to json in the console with 2 char spacing const jsonstr = JSON.stringify(results, null, 2); console.log(jsonstr); diff --git a/src/cli-validator/utils/printResults.js b/src/cli-validator/utils/printResults.js index b7028049c..976423690 100644 --- a/src/cli-validator/utils/printResults.js +++ b/src/cli-validator/utils/printResults.js @@ -11,9 +11,10 @@ module.exports = function print( chalk, printValidators, reportingStats, - originalFile + originalFile, + errorsOnly ) { - const types = ['errors', 'warnings']; + const types = errorsOnly ? ['errors'] : ['errors', 'warnings']; const colors = { errors: 'bgRed', warnings: 'bgYellow' diff --git a/test/cli-validator/tests/optionHandling.js b/test/cli-validator/tests/optionHandling.js index c8162b3e4..b332eae09 100644 --- a/test/cli-validator/tests/optionHandling.js +++ b/test/cli-validator/tests/optionHandling.js @@ -97,6 +97,27 @@ describe('cli tool - test option handling', function() { expect(validatorsPrinted).toEqual(true); }); + it('should print only errors when the -e command is given', async function() { + const capturedText = []; + + const unhookIntercept = intercept(function(txt) { + capturedText.push(stripAnsiFrom(txt)); + return ''; + }); + + const program = {}; + program.args = ['./test/cli-validator/mockFiles/errAndWarn.yaml']; + program.errors_only = true; + program.default_mode = true; + + await commandLineValidator(program); + unhookIntercept(); + + capturedText.forEach(function(line) { + expect(line.includes('warnings')).toEqual(false); + }); + }); + it('should print correct statistics report when -s option is given', async function() { const capturedText = []; @@ -223,6 +244,30 @@ describe('cli tool - test option handling', function() { ); }); + it('should print only errors as json output when -j -e option is given', async function() { + const capturedText = []; + + const unhookIntercept = intercept(function(txt) { + capturedText.push(stripAnsiFrom(txt)); + return ''; + }); + + const program = {}; + program.args = ['./test/cli-validator/mockFiles/errAndWarn.yaml']; + program.json = true; + program.errors_only = true; + program.default_mode = true; + await commandLineValidator(program); + unhookIntercept(); + + // capturedText should be JSON object. convert to json and check fields + const outputObject = JSON.parse(capturedText); + + expect(outputObject.warning).toEqual(false); + expect(outputObject.error).toEqual(true); + expect(outputObject.warnings).toEqual(undefined); + }); + it('should change output for overridden options when config file is manually specified', async function() { const capturedText = [];