Skip to content

Commit 44dd740

Browse files
committed
Add type: array validation for Header objects
1 parent b3d39ec commit 44dd740

File tree

2 files changed

+86
-0
lines changed

2 files changed

+86
-0
lines changed

src/plugins/validation/semantic-validators/validators/items-required-for-array-objects.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,15 @@ export function validate({ resolvedSpec }) {
4343

4444
}
4545

46+
if(path[path.length - 2] === "headers") {
47+
if(obj.type === "array" && typeof obj.items !== "object") {
48+
errors.push({
49+
path,
50+
message: "Headers with 'array' type require an 'items' property"
51+
})
52+
}
53+
}
54+
4655
if(Object.keys(obj).length) {
4756
return Object.keys(obj).map(k => walk(obj[k], [...path, k]))
4857

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import expect from "expect"
2+
import { validate } from "plugins/validation/semantic-validators/validators/items-required-for-array-objects"
3+
4+
describe("validation plugin - semantic - items required for array objects", () => {
5+
it("should return an error when an array header object omits an `items` property", () => {
6+
const spec = {
7+
"swagger": "2.0",
8+
"info": {
9+
"version": "1.0.0",
10+
"title": "Swagger Petstore"
11+
},
12+
"paths": {
13+
"/pets": {
14+
"get": {
15+
"description": "Returns all pets from the system that the user has access to",
16+
"responses": {
17+
"200": {
18+
"description": "pet response",
19+
"headers": {
20+
"X-MyHeader": {
21+
"type": "array"
22+
}
23+
}
24+
},
25+
"default": {
26+
"description": "unexpected error"
27+
}
28+
}
29+
}
30+
}
31+
}
32+
}
33+
34+
let res = validate({ resolvedSpec: spec })
35+
expect(res.errors.length).toEqual(1)
36+
expect(res.errors[0].path).toEqual(["paths", "/pets", "get", "responses", "200", "headers", "X-MyHeader"])
37+
expect(res.errors[0].message).toEqual("Headers with 'array' type require an 'items' property")
38+
expect(res.warnings.length).toEqual(0)
39+
})
40+
41+
it("should not return an error when an array header object has an `items` property", () => {
42+
const spec = {
43+
"swagger": "2.0",
44+
"info": {
45+
"version": "1.0.0",
46+
"title": "Swagger Petstore"
47+
},
48+
"paths": {
49+
"/pets": {
50+
"get": {
51+
"description": "Returns all pets from the system that the user has access to",
52+
"responses": {
53+
"200": {
54+
"description": "pet response",
55+
"headers": {
56+
"X-MyHeader": {
57+
"type": "array",
58+
"items": {
59+
"type": "string"
60+
}
61+
}
62+
}
63+
},
64+
"default": {
65+
"description": "unexpected error"
66+
}
67+
}
68+
}
69+
}
70+
}
71+
}
72+
73+
let res = validate({ resolvedSpec: spec })
74+
expect(res.errors.length).toEqual(0)
75+
expect(res.warnings.length).toEqual(0)
76+
})
77+
})

0 commit comments

Comments
 (0)