Skip to content

Commit fd77899

Browse files
authored
Merge pull request #37 from SamerJaser96/missing_info_validation
feat: added check in the validator for info section.
2 parents 4287f4b + 8c6a88f commit fd77899

File tree

2 files changed

+134
-0
lines changed
  • src/plugins/validation/2and3/semantic-validators
  • test/plugins/validation/2and3

2 files changed

+134
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Assertation 1:
2+
// check if info exists
3+
4+
// Assertation 2:
5+
// making sure that the required version and title are defined properly
6+
module.exports.validate = function({ jsSpec }) {
7+
const errors = [];
8+
const warnings = [];
9+
10+
const info = jsSpec.info;
11+
const hasInfo = info && typeof info === 'object';
12+
if (!hasInfo) {
13+
errors.push({
14+
path: ['info'],
15+
message: 'API definition must have an `info` object'
16+
});
17+
} else {
18+
const title = jsSpec.info.title;
19+
const hasTitle =
20+
typeof title === 'string' && title.toString().trim().length > 0;
21+
const version = jsSpec.info.version;
22+
const hasVersion =
23+
typeof version === 'string' && version.toString().trim().length > 0;
24+
25+
if (!hasTitle) {
26+
errors.push({
27+
path: ['info', 'title'],
28+
message: '`info` object must have a string-type `title` field'
29+
});
30+
} else if (!hasVersion) {
31+
errors.push({
32+
path: ['info', 'version'],
33+
message: '`info` object must have a string-type `version` field'
34+
});
35+
}
36+
}
37+
return { errors, warnings };
38+
};

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

+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
const config = require('../../../../src/.defaultsForValidator').defaults.shared;
2+
const expect = require('expect');
3+
const {
4+
validate
5+
} = require('../../../../src/plugins/validation/2and3/semantic-validators/info');
6+
7+
describe('validation plugin - semantic - info', () => {
8+
//this is for openapi object
9+
it('should return an error when a parameter does not have info', () => {
10+
const spec = {
11+
Openapi: '3.0.0'
12+
};
13+
14+
const res = validate({ jsSpec: spec }, config);
15+
expect(res.errors.length).toEqual(1);
16+
expect(res.errors[0].path).toEqual(['info']);
17+
expect(res.errors[0].message).toEqual(
18+
'API definition must have an `info` object'
19+
);
20+
});
21+
it('should return an error when a info is not defined as a proper object', () => {
22+
const spec = {
23+
Openapi: '3.0.0',
24+
info: 'abc'
25+
};
26+
27+
const res = validate({ jsSpec: spec }, config);
28+
expect(res.errors.length).toEqual(1);
29+
expect(res.errors[0].path).toEqual(['info']);
30+
expect(res.errors[0].message).toEqual(
31+
'API definition must have an `info` object'
32+
);
33+
});
34+
it('should return an error when a title and version is not a string', () => {
35+
const spec = {
36+
Openapi: '3.0.0',
37+
info: {
38+
title: 32,
39+
version: '32'
40+
}
41+
};
42+
43+
const res = validate({ jsSpec: spec }, config);
44+
expect(res.errors.length).toEqual(1);
45+
expect(res.errors[0].path).toEqual(['info', 'title']);
46+
expect(res.errors[0].message).toEqual(
47+
'`info` object must have a string-type `title` field'
48+
);
49+
});
50+
it('should return an error when a title and version is not a string', () => {
51+
const spec = {
52+
Openapi: '3.0.0',
53+
info: {
54+
title: '32',
55+
version: 32
56+
}
57+
};
58+
59+
const res = validate({ jsSpec: spec }, config);
60+
expect(res.errors.length).toEqual(1);
61+
expect(res.errors[0].path).toEqual(['info', 'version']);
62+
expect(res.errors[0].message).toEqual(
63+
'`info` object must have a string-type `version` field'
64+
);
65+
});
66+
it('should return an error when a title is missing', () => {
67+
const spec = {
68+
Openapi: '3.0.0',
69+
info: {
70+
version: '32'
71+
}
72+
};
73+
74+
const res = validate({ jsSpec: spec }, config);
75+
expect(res.errors.length).toEqual(1);
76+
expect(res.errors[0].path).toEqual(['info', 'title']);
77+
expect(res.errors[0].message).toEqual(
78+
'`info` object must have a string-type `title` field'
79+
);
80+
});
81+
it('should return an error when a version is missing', () => {
82+
const spec = {
83+
Openapi: '3.0.0',
84+
info: {
85+
title: '32'
86+
}
87+
};
88+
89+
const res = validate({ jsSpec: spec }, config);
90+
expect(res.errors.length).toEqual(1);
91+
expect(res.errors[0].path).toEqual(['info', 'version']);
92+
expect(res.errors[0].message).toEqual(
93+
'`info` object must have a string-type `version` field'
94+
);
95+
});
96+
});

0 commit comments

Comments
 (0)