Skip to content

Commit 20f0911

Browse files
authored
fix: make $ref pattern check configurable - new rule incorrect_ref_pattern (#78)
1 parent 6c5accc commit 20f0911

File tree

4 files changed

+36
-33
lines changed

4 files changed

+36
-33
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,7 @@ The supported rules are described below:
255255
| has_circular_references | Flag any circular references found in the Swagger spec. | shared |
256256
| $ref_siblings | Flag any properties that are siblings of a `$ref` property. | shared |
257257
| duplicate_sibling_description | Flag descriptions sibling to `$ref` if identical to referenced description. | shared |
258+
| incorrect_ref_pattern | Flag internal `$ref` values that do not point to the section they should (e.g. referencing `parameters` from a `schema` field). | shared |
258259

259260
[1]: https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#dataTypeFormat
260261
[2]: https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md#parameter-object
@@ -407,6 +408,7 @@ The default values for each rule are described below.
407408
| has_circular_references | warning |
408409
| $ref_siblings | off |
409410
| duplicate_sibling_description | warning |
411+
| incorrect_ref_pattern | warning |
410412

411413

412414
## Turning off `update-notifier`

src/.defaultsForValidator.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ const defaults = {
6666
'no_empty_descriptions': 'error',
6767
'has_circular_references': 'warning',
6868
'$ref_siblings': 'off',
69-
'duplicate_sibling_description': 'warning'
69+
'duplicate_sibling_description': 'warning',
70+
'incorrect_ref_pattern': 'warning'
7071
}
7172
},
7273
'swagger2': {

src/plugins/validation/2and3/semantic-validators/walker.js

+23-28
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ const match = require('matcher');
1515
const walk = require('../../../utils/walk');
1616

1717
module.exports.validate = function({ jsSpec, isOAS3 }, config) {
18-
const errors = [];
19-
const warnings = [];
18+
const result = {};
19+
result.error = [];
20+
result.warning = [];
2021

2122
config = config.walker;
2223

@@ -44,7 +45,7 @@ module.exports.validate = function({ jsSpec, isOAS3 }, config) {
4445
///// "type" should always have a string-type value, everywhere.
4546
if (obj.type && allowedParents.indexOf(path[path.length - 1]) === -1) {
4647
if (typeof obj.type !== 'string') {
47-
errors.push({
48+
result.error.push({
4849
path: [...path, 'type'],
4950
message: '"type" should be a string'
5051
});
@@ -55,7 +56,7 @@ module.exports.validate = function({ jsSpec, isOAS3 }, config) {
5556

5657
if (obj.maximum && obj.minimum) {
5758
if (greater(obj.minimum, obj.maximum)) {
58-
errors.push({
59+
result.error.push({
5960
path: path.concat(['minimum']),
6061
message: 'Minimum cannot be more than maximum'
6162
});
@@ -64,7 +65,7 @@ module.exports.validate = function({ jsSpec, isOAS3 }, config) {
6465

6566
if (obj.maxProperties && obj.minProperties) {
6667
if (greater(obj.minProperties, obj.maxProperties)) {
67-
errors.push({
68+
result.error.push({
6869
path: path.concat(['minProperties']),
6970
message: 'minProperties cannot be more than maxProperties'
7071
});
@@ -73,7 +74,7 @@ module.exports.validate = function({ jsSpec, isOAS3 }, config) {
7374

7475
if (obj.maxLength && obj.minLength) {
7576
if (greater(obj.minLength, obj.maxLength)) {
76-
errors.push({
77+
result.error.push({
7778
path: path.concat(['minLength']),
7879
message: 'minLength cannot be more than maxLength'
7980
});
@@ -89,39 +90,33 @@ module.exports.validate = function({ jsSpec, isOAS3 }, config) {
8990
if (refBlacklist && refBlacklist.length && matches.length) {
9091
// Assertation 2
9192
// use the slice(1) to remove the `!` negator from the string
92-
warnings.push({
93-
path: [...path, '$ref'],
94-
message: `${
95-
blacklistPayload.location
96-
} $refs must follow this format: ${refBlacklist[0].slice(1)}`
97-
});
93+
const checkStatus = config.incorrect_ref_pattern;
94+
if (checkStatus !== 'off') {
95+
result[checkStatus].push({
96+
path: [...path, '$ref'],
97+
message: `${
98+
blacklistPayload.location
99+
} $refs must follow this format: ${refBlacklist[0].slice(1)}`
100+
});
101+
}
98102
}
99103
}
100104

101105
const keys = Object.keys(obj);
102106
keys.forEach(k => {
103107
if (keys.indexOf('$ref') > -1 && k !== '$ref') {
104-
switch (config.$ref_siblings) {
105-
case 'error':
106-
errors.push({
107-
path: path.concat([k]),
108-
message: 'Values alongside a $ref will be ignored.'
109-
});
110-
break;
111-
case 'warning':
112-
warnings.push({
113-
path: path.concat([k]),
114-
message: 'Values alongside a $ref will be ignored.'
115-
});
116-
break;
117-
default:
118-
break;
108+
const checkStatus = config.$ref_siblings;
109+
if (checkStatus !== 'off') {
110+
result[checkStatus].push({
111+
path: path.concat([k]),
112+
message: 'Values alongside a $ref will be ignored.'
113+
});
119114
}
120115
}
121116
});
122117
});
123118

124-
return { errors, warnings };
119+
return { errors: result.error, warnings: result.warning };
125120
};
126121

127122
// values are globs!

test/plugins/validation/2and3/walker.js

+9-4
Original file line numberDiff line numberDiff line change
@@ -586,11 +586,15 @@ describe('validation plugin - semantic - spec walker', () => {
586586
}
587587
};
588588

589-
const updatedConfig = Object.assign(config, {
590-
walker: { $ref_siblings: 'warning' }
591-
});
589+
// temporarily configure $ref_siblings to be a warning
590+
const originalValue = config.walker.$ref_siblings;
591+
config.walker.$ref_siblings = 'warning';
592+
593+
const res = validate({ jsSpec: spec }, config);
594+
595+
// revert changes to config to avoid affecting other tests
596+
config.walker.$ref_siblings = originalValue;
592597

593-
const res = validate({ jsSpec: spec }, updatedConfig);
594598
expect(res.errors.length).toEqual(0);
595599
expect(res.warnings.length).toEqual(1);
596600
expect(res.warnings[0].path).toEqual([
@@ -600,6 +604,7 @@ describe('validation plugin - semantic - spec walker', () => {
600604
'description'
601605
]);
602606
});
607+
603608
it('should return a problem for a links $ref that does not have the correct format', function() {
604609
const spec = {
605610
paths: {

0 commit comments

Comments
 (0)