Skip to content
This repository was archived by the owner on Nov 8, 2024. It is now read-only.

Commit c6dea60

Browse files
committed
fix(oas2): collapse multiple object required property errors
1 parent 922af4a commit c6dea60

File tree

3 files changed

+21
-89
lines changed

3 files changed

+21
-89
lines changed

packages/openapi2-parser/CHANGELOG.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88
does not contain the `swagger` key, in prior versions the parser may have
99
crashed under some inputs.
1010

11-
- Improved clarity of validation error messages for Schema Object 'type'
12-
property.
11+
- Improved clarity of validation error messages.
1312

1413
## 0.32.0 (2020-06-12)
1514

packages/openapi2-parser/lib/parser.js

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1550,6 +1550,15 @@ class Parser {
15501550
createValidationAnnotation(error) {
15511551
let message;
15521552

1553+
// join array of words
1554+
const oxfordJoin = array => array.map((item, index) => {
1555+
if (index === array.length - 1) {
1556+
return `or '${item}'`;
1557+
}
1558+
1559+
return `'${item}'`;
1560+
}).join(', ');
1561+
15531562
if (error.code === 'ANY_OF_MISSING'
15541563
&& _.last(error.path) === 'type'
15551564
&& error.inner.length === 2
@@ -1577,15 +1586,17 @@ class Parser {
15771586
return;
15781587
}
15791588

1580-
if (error.code === 'ENUM_MISMATCH') {
1581-
const enumerations = error[ZSchema.schemaSymbol].enum.map((item, index, items) => {
1582-
if (index === items.length - 1) {
1583-
return `or '${item}'`;
1584-
}
1585-
1586-
return `'${item}'`;
1587-
}).join(', ');
1589+
if (error.code === 'ONE_OF_MISSING'
1590+
&& error.inner.every(subError => subError.code === 'OBJECT_MISSING_REQUIRED_PROPERTY')) {
1591+
// collase a collection of "one of" required property validations into one message
1592+
const missingProperties = oxfordJoin(error.inner.map(subError => subError.params[0]));
1593+
message = `Object must contain either ${missingProperties} properties`;
1594+
this.createAnnotation(annotations.VALIDATION_ERROR, error.path, message);
1595+
return;
1596+
}
15881597

1598+
if (error.code === 'ENUM_MISMATCH') {
1599+
const enumerations = oxfordJoin(error[ZSchema.schemaSymbol].enum);
15891600
const value = error.params[0];
15901601
message = `Value must be either ${enumerations} not '${value}'`;
15911602
} else {

packages/openapi2-parser/test/fixtures/schema-error-parameter.json

Lines changed: 1 addition & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -81,85 +81,7 @@
8181
]
8282
}
8383
},
84-
"content": "Data does not match any schemas from 'oneOf'"
85-
},
86-
{
87-
"element": "annotation",
88-
"meta": {
89-
"classes": {
90-
"element": "array",
91-
"content": [
92-
{
93-
"element": "string",
94-
"content": "error"
95-
}
96-
]
97-
},
98-
"links": {
99-
"element": "array",
100-
"content": [
101-
{
102-
"element": "link",
103-
"attributes": {
104-
"relation": {
105-
"element": "string",
106-
"content": "origin"
107-
},
108-
"href": {
109-
"element": "string",
110-
"content": "http://docs.apiary.io/validations/swagger#swagger-validation"
111-
}
112-
}
113-
}
114-
]
115-
}
116-
},
117-
"attributes": {
118-
"code": {
119-
"element": "number",
120-
"content": 4
121-
}
122-
},
123-
"content": "Missing required property: schema"
124-
},
125-
{
126-
"element": "annotation",
127-
"meta": {
128-
"classes": {
129-
"element": "array",
130-
"content": [
131-
{
132-
"element": "string",
133-
"content": "error"
134-
}
135-
]
136-
},
137-
"links": {
138-
"element": "array",
139-
"content": [
140-
{
141-
"element": "link",
142-
"attributes": {
143-
"relation": {
144-
"element": "string",
145-
"content": "origin"
146-
},
147-
"href": {
148-
"element": "string",
149-
"content": "http://docs.apiary.io/validations/swagger#swagger-validation"
150-
}
151-
}
152-
}
153-
]
154-
}
155-
},
156-
"attributes": {
157-
"code": {
158-
"element": "number",
159-
"content": 4
160-
}
161-
},
162-
"content": "Missing required property: type"
84+
"content": "Object must contain either 'schema', or 'type' properties"
16385
}
16486
]
16587
}

0 commit comments

Comments
 (0)