Skip to content

Commit a338bf0

Browse files
SamerJaser96dpopp07
authored andcommitted
feat: new cli option, --errors_only, for printing only the errors (#68)
1 parent 3c95271 commit a338bf0

File tree

6 files changed

+74
-6
lines changed

6 files changed

+74
-6
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ To build these, run `npm run pkg` in the root. The binaries (lint-openapi-linux
6262

6363
#### [options]
6464
- -s (--report_statistics) : Print a simple report at the end of the output showing the frequency, in percentage, of each error/warning.
65+
- -e (--errors_only) : Only print the errors, ignore the warnings.
6566
- -d (--default_mode) : This option turns off [configuration](#configuration) and runs the validator in [default mode](#default-mode).
6667
- -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.
6768
- -n (--no_colors) : The output is colored by default. If this bothers you, this flag will turn off the coloring.

src/cli-validator/index.js

+4
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ program
3838
)
3939
.option(
4040
'-c, --config <file>', 'path to config file, used instead of .validaterc if provided'
41+
)
42+
.option(
43+
'-e, --errors_only',
44+
'only print the errors, ignore the warnings'
4145
);
4246

4347
/* prettier-ignore */

src/cli-validator/runValidator.js

+16-2
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ const processInput = async function(program) {
3737
const turnOffColoring = !!program.no_colors;
3838
const defaultMode = !!program.default_mode;
3939
const jsonOutput = !!program.json;
40+
const errorsOnly = !!program.errors_only;
4041

4142
const configFileOverride = program.config;
4243

@@ -231,11 +232,24 @@ const processInput = async function(program) {
231232
continue;
232233
}
233234

235+
// the warning property tells the user if warnings are included as part of the output
236+
// if errorsOnly is true, only errors will be returned, so need to force this to false
237+
if (errorsOnly) {
238+
results.warning = false;
239+
}
240+
234241
if (jsonOutput) {
235-
printJson(results, originalFile);
242+
printJson(results, originalFile, errorsOnly);
236243
} else {
237244
if (results.error || results.warning) {
238-
print(results, chalk, printValidators, reportingStats, originalFile);
245+
print(
246+
results,
247+
chalk,
248+
printValidators,
249+
reportingStats,
250+
originalFile,
251+
errorsOnly
252+
);
239253
// fail on errors, but not if there are only warnings
240254
if (results.error) exitCode = 1;
241255
} else {

src/cli-validator/utils/printJsonResults.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ const getLineNumberForPath = require(__dirname + '/../../plugins/ast/ast')
55
.getLineNumberForPath;
66

77
// function to print the results as json to the console.
8-
module.exports = function printJson(results, originalFile) {
9-
const types = ['errors', 'warnings'];
8+
module.exports = function printJson(results, originalFile, errorsOnly) {
9+
const types = errorsOnly ? ['errors'] : ['errors', 'warnings'];
1010
types.forEach(type => {
1111
each(results[type], problems => {
1212
problems.forEach(problem => {
@@ -32,6 +32,9 @@ module.exports = function printJson(results, originalFile) {
3232
});
3333
});
3434
});
35+
if (errorsOnly) {
36+
delete results.warnings;
37+
}
3538
// render the results to json in the console with 2 char spacing
3639
const jsonstr = JSON.stringify(results, null, 2);
3740
console.log(jsonstr);

src/cli-validator/utils/printResults.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ module.exports = function print(
1111
chalk,
1212
printValidators,
1313
reportingStats,
14-
originalFile
14+
originalFile,
15+
errorsOnly
1516
) {
16-
const types = ['errors', 'warnings'];
17+
const types = errorsOnly ? ['errors'] : ['errors', 'warnings'];
1718
const colors = {
1819
errors: 'bgRed',
1920
warnings: 'bgYellow'

test/cli-validator/tests/optionHandling.js

+45
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,27 @@ describe('cli tool - test option handling', function() {
9797
expect(validatorsPrinted).toEqual(true);
9898
});
9999

100+
it('should print only errors when the -e command is given', async function() {
101+
const capturedText = [];
102+
103+
const unhookIntercept = intercept(function(txt) {
104+
capturedText.push(stripAnsiFrom(txt));
105+
return '';
106+
});
107+
108+
const program = {};
109+
program.args = ['./test/cli-validator/mockFiles/errAndWarn.yaml'];
110+
program.errors_only = true;
111+
program.default_mode = true;
112+
113+
await commandLineValidator(program);
114+
unhookIntercept();
115+
116+
capturedText.forEach(function(line) {
117+
expect(line.includes('warnings')).toEqual(false);
118+
});
119+
});
120+
100121
it('should print correct statistics report when -s option is given', async function() {
101122
const capturedText = [];
102123

@@ -223,6 +244,30 @@ describe('cli tool - test option handling', function() {
223244
);
224245
});
225246

247+
it('should print only errors as json output when -j -e option is given', async function() {
248+
const capturedText = [];
249+
250+
const unhookIntercept = intercept(function(txt) {
251+
capturedText.push(stripAnsiFrom(txt));
252+
return '';
253+
});
254+
255+
const program = {};
256+
program.args = ['./test/cli-validator/mockFiles/errAndWarn.yaml'];
257+
program.json = true;
258+
program.errors_only = true;
259+
program.default_mode = true;
260+
await commandLineValidator(program);
261+
unhookIntercept();
262+
263+
// capturedText should be JSON object. convert to json and check fields
264+
const outputObject = JSON.parse(capturedText);
265+
266+
expect(outputObject.warning).toEqual(false);
267+
expect(outputObject.error).toEqual(true);
268+
expect(outputObject.warnings).toEqual(undefined);
269+
});
270+
226271
it('should change output for overridden options when config file is manually specified', async function() {
227272
const capturedText = [];
228273

0 commit comments

Comments
 (0)