Skip to content

Commit d224641

Browse files
author
Mike Kistler
authored
Add support for "info" and "hint" severity levels (#219)
* feat: add support for info and hint messages * test: batch tests to improve test performance
1 parent 775645b commit d224641

File tree

15 files changed

+467
-69
lines changed

15 files changed

+467
-69
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,8 @@ The object will always have `errors` and `warnings` keys that map to arrays. If
155155

156156
## Configuration
157157
The command line validator is built so that each IBM validation can be configured. To get started configuring the validator, [set up](#setup) a [configuration file](#configuration-file) and continue reading this section.
158-
Specific validation "rules" can be turned off, or configured to trigger either errors or warnings in the validator. Some validations can be configured even further, such as switching the case convention to validate against for parameter names.
158+
Specific validation "rules" can be turned off, or configured to trigger an error, warning, info, or hint message in the validator output.
159+
Some validations can be configured even further, such as switching the case convention to validate against for parameter names.
159160
Additionally, certain files can be ignored by the validator. Any glob placed in a file called `.validateignore` will always be ignored by the validator at runtime. This is set up like a `.gitignore` or a `.eslintignore` file.
160161

161162
### Setup
@@ -298,7 +299,7 @@ The supported rules are described below:
298299

299300
#### Statuses
300301

301-
Each rule can be assigned a status. The supported statuses are `error`, `warning`, and `off`.
302+
Each rule can be assigned a status. The supported statuses are `error`, `warning`, `info`, `hint` and `off`.
302303
Some rules can be configured further with configuration options. The format of this configuration is to provide an array, rather than just a string. e.g.
303304
`"param_name_case_convention": ["error", "lower_camel_case"]`
304305
If just a string is provided for these rule, the default configuration option will be used. If only one value is provided in the array, it **MUST** be a status. The default configuration option will be used in this case as well. The rules that support configuration options will have **two** values in the [defaults](#default-values) table.

src/cli-validator/runValidator.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,12 +277,14 @@ const processInput = async function(program) {
277277
// if errorsOnly is true, only errors will be returned, so need to force this to false
278278
if (errorsOnly) {
279279
results.warning = false;
280+
results.info = false;
281+
results.hint = false;
280282
}
281283

282284
if (jsonOutput) {
283285
printJson(results, originalFile, errorsOnly);
284286
} else {
285-
if (results.error || results.warning) {
287+
if (results.error || results.warning || results.info || results.hint) {
286288
print(
287289
results,
288290
chalk,

src/cli-validator/utils/printResults.js

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,14 @@ module.exports = function print(
1414
originalFile,
1515
errorsOnly
1616
) {
17-
const types = errorsOnly ? ['errors'] : ['errors', 'warnings'];
17+
const types = errorsOnly
18+
? ['errors']
19+
: ['errors', 'warnings', 'infos', 'hints'];
1820
const colors = {
1921
errors: 'bgRed',
20-
warnings: 'bgYellow'
22+
warnings: 'bgYellow',
23+
infos: 'bgGrey',
24+
hints: 'bgGreen'
2125
};
2226

2327
// define an object template in the case that statistics reporting is turned on
@@ -27,6 +31,12 @@ module.exports = function print(
2731
},
2832
warnings: {
2933
total: 0
34+
},
35+
infos: {
36+
total: 0
37+
},
38+
hints: {
39+
total: 0
3040
}
3141
};
3242

@@ -91,8 +101,19 @@ module.exports = function print(
91101
chalk.cyan(` Total number of errors : ${stats.errors.total}`)
92102
);
93103
console.log(
94-
chalk.cyan(` Total number of warnings : ${stats.warnings.total}\n`)
104+
chalk.cyan(` Total number of warnings : ${stats.warnings.total}`)
95105
);
106+
if (stats.infos.total > 0) {
107+
console.log(
108+
chalk.cyan(` Total number of infos : ${stats.infos.total}`)
109+
);
110+
}
111+
if (stats.hints.total > 0) {
112+
console.log(
113+
chalk.cyan(` Total number of hints : ${stats.hints.total}`)
114+
);
115+
}
116+
console.log('');
96117

97118
types.forEach(type => {
98119
// print the type, either error or warning

src/cli-validator/utils/processConfiguration.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,8 @@ const validateConfigObject = function(configObject, chalk) {
100100
return; // skip statuses for invalid rule
101101
}
102102

103-
// check that all statuses are valid (either 'error', 'warning', or 'off')
104-
const allowedStatusValues = ['error', 'warning', 'off'];
103+
// check that all statuses are valid (either 'error', 'warning', 'info', 'hint' or 'off')
104+
const allowedStatusValues = ['error', 'warning', 'info', 'hint', 'off'];
105105
let userStatus = configObject[spec][category][rule];
106106

107107
// if the rule supports an array in configuration,

src/cli-validator/utils/validator.js

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,12 @@ module.exports = function validateSwagger(
4343
const validationResults = {
4444
errors: {},
4545
warnings: {},
46+
infos: {},
47+
hints: {},
4648
error: false,
47-
warning: false
49+
warning: false,
50+
info: false,
51+
hint: false
4852
};
4953

5054
// use version specific and shared validations
@@ -66,6 +70,14 @@ module.exports = function validateSwagger(
6670
validationResults.warnings[key] = [...parsedSpectralResults.warnings];
6771
validationResults.warning = true;
6872
}
73+
if (parsedSpectralResults.infos.length) {
74+
validationResults.infos[key] = [...parsedSpectralResults.infos];
75+
validationResults.info = true;
76+
}
77+
if (parsedSpectralResults.hints.length) {
78+
validationResults.hints[key] = [...parsedSpectralResults.hints];
79+
validationResults.hint = true;
80+
}
6981

7082
// run circular reference validator
7183
if (allSpecs.circular) {
@@ -93,6 +105,14 @@ module.exports = function validateSwagger(
93105
validationResults.warnings[key] = [...problem.warnings];
94106
validationResults.warning = true;
95107
}
108+
if (problem.infos.length) {
109+
validationResults.infos[key] = [...problem.infos];
110+
validationResults.info = true;
111+
}
112+
if (problem.hints.length) {
113+
validationResults.hints[key] = [...problem.hints];
114+
validationResults.hint = true;
115+
}
96116
});
97117

98118
Object.keys(sharedSemanticValidators).forEach(key => {
@@ -111,6 +131,20 @@ module.exports = function validateSwagger(
111131
);
112132
validationResults.warning = true;
113133
}
134+
if (problem.infos.length) {
135+
validationResults.infos[key] = [].concat(
136+
validationResults.infos[key] || [],
137+
problem.infos
138+
);
139+
validationResults.info = true;
140+
}
141+
if (problem.hints.length) {
142+
validationResults.hints[key] = [].concat(
143+
validationResults.hints[key] || [],
144+
problem.hints
145+
);
146+
validationResults.hint = true;
147+
}
114148
});
115149

116150
// run structural validator

src/plugins/utils/messageCarrier.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ module.exports = class MessageCarrier {
44
constructor() {
55
this._messages = {
66
error: [],
7-
warning: []
7+
warning: [],
8+
info: [],
9+
hint: []
810
};
911
}
1012

@@ -20,7 +22,15 @@ module.exports = class MessageCarrier {
2022
return this._messages.warning;
2123
}
2224

23-
// status should be 'off', 'error', or 'warning'
25+
get infos() {
26+
return this._messages.info;
27+
}
28+
29+
get hints() {
30+
return this._messages.hint;
31+
}
32+
33+
// status should be 'off', 'error', 'warning', 'info', or 'hint'
2434
addMessage(path, message, status) {
2535
if (this._messages[status]) {
2636
this._messages[status].push({

src/spectral/utils/spectral-validator.js

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,25 @@ const parseResults = function(results, debug) {
1717
const message = validationResult['message'];
1818
const path = validationResult['path'];
1919

20+
if (code === 'parser') {
21+
// Spectral doesn't allow disabling parser rules, so don't include them
22+
// in the output (for now)
23+
continue;
24+
}
25+
2026
if (typeof severity === 'number' && code && message && path) {
21-
if (code === 'parser') {
22-
// Spectral doesn't allow disabling parser rules, so don't include them
23-
// in the output (for now)
24-
continue;
25-
}
26-
// Our validator only supports warning/error level, so only include
27-
// those validation results (for now)
28-
if (severity === 1) {
29-
// warning
30-
messages.addMessage(path, message, 'warning');
31-
} else if (severity === 0) {
27+
if (severity === 0) {
3228
// error
3329
messages.addMessage(path, message, 'error');
30+
} else if (severity === 1) {
31+
// warning
32+
messages.addMessage(path, message, 'warning');
33+
} else if (severity === 2) {
34+
// info
35+
messages.addMessage(path, message, 'info');
36+
} else if (severity === 3) {
37+
// hint
38+
messages.addMessage(path, message, 'hint');
3439
}
3540
} else {
3641
if (debug) {

0 commit comments

Comments
 (0)