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

Commit 5fc917a

Browse files
committed
fix(oas2): return only enum failure for type validation
1 parent 656ba9f commit 5fc917a

File tree

3 files changed

+20
-164
lines changed

3 files changed

+20
-164
lines changed

packages/openapi2-parser/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
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.
13+
1114
## 0.32.0 (2020-06-12)
1215

1316
The package has been updated for compatibility with `@apielements/core`.

packages/openapi2-parser/lib/parser.js

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

1553+
if (error.code === 'ANY_OF_MISSING'
1554+
&& _.last(error.path) === 'type'
1555+
&& error.inner.length === 2
1556+
&& error.inner[0].code === 'ENUM_MISMATCH'
1557+
&& error.inner[1].code === 'INVALID_TYPE'
1558+
) {
1559+
// Schema Object 'type' validation can contain enum mismatch and type mismatch
1560+
// Prefer only the enum mismatch for less confusing error message
1561+
// Schema is something like the following which causes anyOf validation
1562+
// with two inner validation errors:
1563+
// anyOf:
1564+
// - enum: ['array', 'number', ...]
1565+
// - type: ['string', 'array']
1566+
this.createValidationAnnotation(error.inner[0]);
1567+
return;
1568+
}
1569+
15531570
if (error.code === 'ENUM_MISMATCH') {
15541571
const enumerations = error[ZSchema.schemaSymbol].enum.map((item, index, items) => {
15551572
if (index === items.length - 1) {
Lines changed: 0 additions & 164 deletions
Original file line numberDiff line numberDiff line change
@@ -1,88 +1,6 @@
11
{
22
"element": "parseResult",
33
"content": [
4-
{
5-
"element": "annotation",
6-
"meta": {
7-
"classes": {
8-
"element": "array",
9-
"content": [
10-
{
11-
"element": "string",
12-
"content": "error"
13-
}
14-
]
15-
},
16-
"links": {
17-
"element": "array",
18-
"content": [
19-
{
20-
"element": "link",
21-
"attributes": {
22-
"relation": {
23-
"element": "string",
24-
"content": "origin"
25-
},
26-
"href": {
27-
"element": "string",
28-
"content": "http://docs.apiary.io/validations/swagger#swagger-validation"
29-
}
30-
}
31-
}
32-
]
33-
}
34-
},
35-
"attributes": {
36-
"code": {
37-
"element": "number",
38-
"content": 4
39-
},
40-
"sourceMap": {
41-
"element": "array",
42-
"content": [
43-
{
44-
"element": "sourceMap",
45-
"content": [
46-
{
47-
"element": "array",
48-
"content": [
49-
{
50-
"element": "number",
51-
"attributes": {
52-
"line": {
53-
"element": "number",
54-
"content": 7
55-
},
56-
"column": {
57-
"element": "number",
58-
"content": 4
59-
}
60-
},
61-
"content": 106
62-
},
63-
{
64-
"element": "number",
65-
"attributes": {
66-
"line": {
67-
"element": "number",
68-
"content": 7
69-
},
70-
"column": {
71-
"element": "number",
72-
"content": 13
73-
}
74-
},
75-
"content": 9
76-
}
77-
]
78-
}
79-
]
80-
}
81-
]
82-
}
83-
},
84-
"content": "Data does not match any schemas from 'anyOf'"
85-
},
864
{
875
"element": "annotation",
886
"meta": {
@@ -164,88 +82,6 @@
16482
}
16583
},
16684
"content": "Value must be either 'array', 'boolean', 'integer', 'null', 'number', 'object', or 'string' not 'foo'"
167-
},
168-
{
169-
"element": "annotation",
170-
"meta": {
171-
"classes": {
172-
"element": "array",
173-
"content": [
174-
{
175-
"element": "string",
176-
"content": "error"
177-
}
178-
]
179-
},
180-
"links": {
181-
"element": "array",
182-
"content": [
183-
{
184-
"element": "link",
185-
"attributes": {
186-
"relation": {
187-
"element": "string",
188-
"content": "origin"
189-
},
190-
"href": {
191-
"element": "string",
192-
"content": "http://docs.apiary.io/validations/swagger#swagger-validation"
193-
}
194-
}
195-
}
196-
]
197-
}
198-
},
199-
"attributes": {
200-
"code": {
201-
"element": "number",
202-
"content": 4
203-
},
204-
"sourceMap": {
205-
"element": "array",
206-
"content": [
207-
{
208-
"element": "sourceMap",
209-
"content": [
210-
{
211-
"element": "array",
212-
"content": [
213-
{
214-
"element": "number",
215-
"attributes": {
216-
"line": {
217-
"element": "number",
218-
"content": 7
219-
},
220-
"column": {
221-
"element": "number",
222-
"content": 4
223-
}
224-
},
225-
"content": 106
226-
},
227-
{
228-
"element": "number",
229-
"attributes": {
230-
"line": {
231-
"element": "number",
232-
"content": 7
233-
},
234-
"column": {
235-
"element": "number",
236-
"content": 13
237-
}
238-
},
239-
"content": 9
240-
}
241-
]
242-
}
243-
]
244-
}
245-
]
246-
}
247-
},
248-
"content": "Expected type array but found type string"
24985
}
25086
]
25187
}

0 commit comments

Comments
 (0)