Skip to content

Commit 8c6a88f

Browse files
author
System Administrator
committed
refomat code
1 parent 996024f commit 8c6a88f

File tree

2 files changed

+89
-93
lines changed
  • src/plugins/validation/2and3/semantic-validators
  • test/plugins/validation/2and3

2 files changed

+89
-93
lines changed

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

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,13 @@
66
module.exports.validate = function({ jsSpec }) {
77
const errors = [];
88
const warnings = [];
9+
910
const info = jsSpec.info;
10-
if (!info) {
11+
const hasInfo = info && typeof info === 'object';
12+
if (!hasInfo) {
1113
errors.push({
1214
path: ['info'],
13-
message: 'Missing info object for api defintion'
14-
});
15-
} else if (typeof info != 'object') {
16-
errors.push({
17-
path: ['info'],
18-
message: 'Info is an obect and must have properties'
15+
message: 'API definition must have an `info` object'
1916
});
2017
} else {
2118
const title = jsSpec.info.title;
@@ -24,15 +21,16 @@ module.exports.validate = function({ jsSpec }) {
2421
const version = jsSpec.info.version;
2522
const hasVersion =
2623
typeof version === 'string' && version.toString().trim().length > 0;
24+
2725
if (!hasTitle) {
2826
errors.push({
2927
path: ['info', 'title'],
30-
message: ' Info object is missng the title field'
28+
message: '`info` object must have a string-type `title` field'
3129
});
3230
} else if (!hasVersion) {
3331
errors.push({
3432
path: ['info', 'version'],
35-
message: ' Info object is missng the version field'
33+
message: '`info` object must have a string-type `version` field'
3634
});
3735
}
3836
}

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

Lines changed: 82 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -4,95 +4,93 @@ const {
44
validate
55
} = require('../../../../src/plugins/validation/2and3/semantic-validators/info');
66

7-
describe('validation plugin - semantic - error handling for incorrect formatting', () => {
8-
describe('there should be errors when the defintion is missing neccesrry fields', function() {
9-
//this is for openapi object
10-
it('should return an error when a parameter does not have info', () => {
11-
const spec = {
12-
Openapi: '3.0.0'
13-
};
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+
};
1413

15-
const res = validate({ jsSpec: spec }, config);
16-
expect(res.errors.length).toEqual(1);
17-
expect(res.errors[0].path).toEqual(['info']);
18-
expect(res.errors[0].message).toEqual(
19-
'Missing info object for api defintion'
20-
);
21-
});
22-
it('should return an error when a title and version is not a string', () => {
23-
const spec = {
24-
Openapi: '3.0.0',
25-
info: {
26-
title: 32,
27-
version: '32'
28-
}
29-
};
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+
};
3026

31-
const res = validate({ jsSpec: spec }, config);
32-
expect(res.errors.length).toEqual(1);
33-
expect(res.errors[0].path).toEqual(['info', 'title']);
34-
expect(res.errors[0].message).toEqual(
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-
};
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+
};
4642

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-
};
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+
};
6158

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-
};
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+
};
7673

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-
};
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+
};
8988

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'
95-
);
96-
});
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+
);
9795
});
9896
});

0 commit comments

Comments
 (0)