Skip to content

Commit b3d39ec

Browse files
committed
Add type: array validation for parameters
1 parent 1fc6aed commit b3d39ec

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed
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: 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)