Skip to content

Commit 74411ee

Browse files
authored
Merge pull request #1265 from shockey/bug/1239-type-array-validation
Add type: array validation for parameters
2 parents 18977cf + 5c24770 commit 74411ee

File tree

4 files changed

+150
-0
lines changed

4 files changed

+150
-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: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Assertation 1:
2+
// The items property for a parameter is required when its type is set to array
3+
4+
export function validate({ resolvedSpec }) {
5+
let errors = []
6+
let warnings = []
7+
8+
function walk(obj, path) {
9+
if(typeof obj !== "object" || obj === null) {
10+
return
11+
}
12+
13+
// 1
14+
if(path[path.length - 2] === "parameters") {
15+
if(obj.type === "array" && typeof obj.items !== "object") {
16+
errors.push({
17+
path,
18+
message: "Parameters with 'array' type require an 'items' property."
19+
})
20+
}
21+
}
22+
23+
if(Object.keys(obj).length) {
24+
return Object.keys(obj).map(k => walk(obj[k], [...path, k]))
25+
26+
} else {
27+
return null
28+
}
29+
30+
}
31+
32+
walk(resolvedSpec, [])
33+
34+
return { errors, warnings }
35+
}
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+
})
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import expect from "expect"
2+
import { validate } from "plugins/validation/semantic-validators/validators/parameters"
3+
4+
describe("validation plugin - semantic - parameters", () => {
5+
it("should return an error when an array type parameter omits an `items` property", () => {
6+
const spec = {
7+
"paths": {
8+
"/pets": {
9+
"get": {
10+
"parameters": [
11+
{
12+
"name": "tags",
13+
"in": "query",
14+
"description": "tags to filter by",
15+
"type": "array"
16+
}
17+
]
18+
}
19+
}
20+
}
21+
}
22+
23+
let res = validate({ resolvedSpec: spec })
24+
expect(res.errors.length).toEqual(1)
25+
expect(res.errors[0].path).toEqual(["paths", "/pets", "get", "parameters", "0"])
26+
expect(res.errors[0].message).toEqual("Parameters with 'array' type require an 'items' property.")
27+
expect(res.warnings.length).toEqual(0)
28+
})
29+
})

0 commit comments

Comments
 (0)