Skip to content

Commit 996024f

Browse files
author
System Administrator
committed
add new validation that checks if info is an object as well as test cases and more compact code
1 parent 28ae64c commit 996024f

File tree

4 files changed

+86
-39
lines changed

4 files changed

+86
-39
lines changed

README.md

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -171,12 +171,6 @@ The supported categories are described below:
171171
Each category contains a group of rules. The spec that each rule applies to is marked in the third column. For the actual configuration structure, see the [default values](#default-values).
172172
The supported rules are described below:
173173

174-
##### rules
175-
| Rule | Description | Spec |
176-
| ---------------------------- | ----------------------------------------------------------------------------------- | -------- |
177-
| no_info | Flag missing info field in api defintion. | shared |
178-
| mistyped_version_or_title | Flag version or title that are not strings. | shared |
179-
180174
##### operations
181175
| Rule | Description | Spec |
182176
| ---------------------------- | ----------------------------------------------------------------------------------- | -------- |
@@ -334,13 +328,6 @@ The default values for each rule are described below.
334328

335329
##### shared
336330

337-
338-
###### operations
339-
| Rule | Default |
340-
| ---------------------------- | ------- |
341-
| no_info | error |
342-
| mistyped_title_or_version | error |
343-
344331
###### operations
345332
| Rule | Default |
346333
| ---------------------------- | ------- |

src/.defaultsForValidator.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,6 @@
2020

2121
const defaults = {
2222
'shared': {
23-
'info':{
24-
'no_info': 'error',
25-
'mistyped_title_or_version': 'error'
26-
},
2723
'operations': {
2824
'no_operation_id': 'warning',
2925
'operation_id_case_convention': ['warning', 'lower_snake_case'],

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

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,35 @@
66
module.exports.validate = function({ jsSpec }) {
77
const errors = [];
88
const warnings = [];
9-
const no_input = !jsSpec.info;
10-
let mistyped_title_or_version = false;
11-
if (no_input) {
9+
const info = jsSpec.info;
10+
if (!info) {
1211
errors.push({
1312
path: ['info'],
1413
message: 'Missing info object for api defintion'
1514
});
16-
}
17-
//Assertion 2
18-
if (
19-
jsSpec.info &&
20-
(typeof jsSpec.info.title != 'string' ||
21-
typeof jsSpec.info.version != 'string')
22-
) {
23-
mistyped_title_or_version = true;
24-
}
25-
if (mistyped_title_or_version) {
15+
} else if (typeof info != 'object') {
2616
errors.push({
27-
path: ['info', 'title'],
28-
message:
29-
'Info object missing proper definitions for title or version properties'
17+
path: ['info'],
18+
message: 'Info is an obect and must have properties'
3019
});
20+
} else {
21+
const title = jsSpec.info.title;
22+
const hasTitle =
23+
typeof title === 'string' && title.toString().trim().length > 0;
24+
const version = jsSpec.info.version;
25+
const hasVersion =
26+
typeof version === 'string' && version.toString().trim().length > 0;
27+
if (!hasTitle) {
28+
errors.push({
29+
path: ['info', 'title'],
30+
message: ' Info object is missng the title field'
31+
});
32+
} else if (!hasVersion) {
33+
errors.push({
34+
path: ['info', 'version'],
35+
message: ' Info object is missng the version field'
36+
});
37+
}
3138
}
32-
return { errors: errors, warnings: warnings };
39+
return { errors, warnings };
3340
};

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

Lines changed: 62 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,28 +13,85 @@ describe('validation plugin - semantic - error handling for incorrect formatting
1313
};
1414

1515
const res = validate({ jsSpec: spec }, config);
16-
//expect(res.warnings.length).toEqual(0);
1716
expect(res.errors.length).toEqual(1);
1817
expect(res.errors[0].path).toEqual(['info']);
1918
expect(res.errors[0].message).toEqual(
2019
'Missing info object for api defintion'
2120
);
2221
});
23-
it('should return an error when a title and version are not strings', () => {
22+
it('should return an error when a title and version is not a string', () => {
2423
const spec = {
2524
Openapi: '3.0.0',
2625
info: {
2726
title: 32,
28-
version: 1
27+
version: '32'
2928
}
3029
};
3130

3231
const res = validate({ jsSpec: spec }, config);
33-
//expect(res.warnings.length).toEqual(0);
3432
expect(res.errors.length).toEqual(1);
3533
expect(res.errors[0].path).toEqual(['info', 'title']);
3634
expect(res.errors[0].message).toEqual(
37-
'Info object missing proper definitions for title or version properties'
35+
' Info object is missng the title field'
36+
);
37+
});
38+
it('should return an error when a title and version is not a string', () => {
39+
const spec = {
40+
Openapi: '3.0.0',
41+
info: {
42+
title: '32',
43+
version: 32
44+
}
45+
};
46+
47+
const res = validate({ jsSpec: spec }, config);
48+
expect(res.errors.length).toEqual(1);
49+
expect(res.errors[0].path).toEqual(['info', 'version']);
50+
expect(res.errors[0].message).toEqual(
51+
' Info object is missng the version field'
52+
);
53+
});
54+
it('should return an error when a title is missing', () => {
55+
const spec = {
56+
Openapi: '3.0.0',
57+
info: {
58+
version: '32'
59+
}
60+
};
61+
62+
const res = validate({ jsSpec: spec }, config);
63+
expect(res.errors.length).toEqual(1);
64+
expect(res.errors[0].path).toEqual(['info', 'title']);
65+
expect(res.errors[0].message).toEqual(
66+
' Info object is missng the title field'
67+
);
68+
});
69+
it('should return an error when a version is missing', () => {
70+
const spec = {
71+
Openapi: '3.0.0',
72+
info: {
73+
title: '32'
74+
}
75+
};
76+
77+
const res = validate({ jsSpec: spec }, config);
78+
expect(res.errors.length).toEqual(1);
79+
expect(res.errors[0].path).toEqual(['info', 'version']);
80+
expect(res.errors[0].message).toEqual(
81+
' Info object is missng the version field'
82+
);
83+
});
84+
it('should return an error when a info is not defined as a proper object', () => {
85+
const spec = {
86+
Openapi: '3.0.0',
87+
info: 'abc'
88+
};
89+
90+
const res = validate({ jsSpec: spec }, config);
91+
expect(res.errors.length).toEqual(1);
92+
expect(res.errors[0].path).toEqual(['info']);
93+
expect(res.errors[0].message).toEqual(
94+
'Info is an obect and must have properties'
3895
);
3996
});
4097
});

0 commit comments

Comments
 (0)