Skip to content

Commit 534bb3f

Browse files
authored
Merge pull request #205 from feenst/update-validation-for-object
Align schema validation with specification for objects
2 parents 21352a8 + 485ebb4 commit 534bb3f

File tree

3 files changed

+100
-1
lines changed

3 files changed

+100
-1
lines changed

lib/validators/spec.js

+8
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,14 @@ function validateRequiredPropertiesExist (schema, schemaId) {
330330
}
331331
}
332332

333+
// The "required" keyword is only applicable for objects
334+
if (Array.isArray(schema.type) && !schema.type.includes("object")) {
335+
return;
336+
}
337+
else if (!Array.isArray(schema.type) && schema.type !== "object") {
338+
return;
339+
}
340+
333341
if (schema.required && Array.isArray(schema.required)) {
334342
let props = {};
335343
collectProperties(schema, props);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
swagger: "2.0"
2+
info:
3+
title: API documentation
4+
version: "1.0.0"
5+
paths:
6+
/product:
7+
get:
8+
responses:
9+
"200":
10+
schema:
11+
$ref: "#/definitions/product"
12+
description: Successful
13+
/products:
14+
get:
15+
responses:
16+
"200":
17+
schema:
18+
$ref: "#/definitions/products"
19+
description: Successful
20+
/mood:
21+
get:
22+
responses:
23+
"200":
24+
schema:
25+
$ref: "#/definitions/mood"
26+
description: Successful
27+
/temperature:
28+
get:
29+
responses:
30+
"200":
31+
schema:
32+
$ref: "#/definitions/temperature"
33+
description: Successful
34+
/age:
35+
get:
36+
responses:
37+
"200":
38+
schema:
39+
$ref: "#/definitions/age"
40+
description: Successful
41+
/hunger:
42+
get:
43+
responses:
44+
"200":
45+
schema:
46+
$ref: "#/definitions/hunger"
47+
description: Successful
48+
definitions:
49+
product:
50+
type: object
51+
properties:
52+
expiration:
53+
type: string
54+
format: date
55+
name:
56+
type: string
57+
weight:
58+
type: number
59+
required:
60+
- name
61+
products:
62+
type: array
63+
items:
64+
$ref: "#/definitions/product"
65+
required:
66+
- items # <--- Should not be validated since type is not object
67+
mood:
68+
type: string
69+
example: nostalgic
70+
required:
71+
- length # <--- Should not be validated since type is not object
72+
temperature:
73+
type: number
74+
example: 86
75+
required:
76+
- precision # <--- Should not be validated since type is not object
77+
age:
78+
type: integer
79+
example: 42
80+
required:
81+
- factors # <--- Should not be validated since type is not object
82+
hunger:
83+
type: boolean
84+
example: true
85+
required:
86+
- truth # <--- Should not be validated since type is not object

test/specs/validate-spec/validate-spec.spec.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,12 @@ describe("Invalid APIs (Swagger 2.0 specification validation)", () => {
140140
valid: false,
141141
file: "array-response-body-no-items.yaml",
142142
error: 'Validation failed. /paths/users/get/responses/200/schema is an array, so it must include an \"items\" schema'
143-
}
143+
},
144+
{
145+
name: "only validate required properties on objects",
146+
valid: true,
147+
file: "only-validate-required-properties-on-objects.yaml"
148+
},
144149
];
145150

146151
it('should pass validation if "options.validate.spec" is false', async () => {

0 commit comments

Comments
 (0)