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

Commit 656ba9f

Browse files
committed
fix(oas2): improve enum validation errors
1 parent 967c957 commit 656ba9f

File tree

2 files changed

+22
-19
lines changed

2 files changed

+22
-19
lines changed

packages/openapi2-parser/lib/parser.js

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -149,23 +149,9 @@ class Parser {
149149
// Non-fatal errors, so let us try and create annotations for them and
150150
// continue with the parsing as best we can.
151151
if (err.details) {
152-
const queue = [err.details];
153-
154-
while (queue.length) {
155-
_.forEach(queue[0], (item) => {
156-
this.createAnnotation(annotations.VALIDATION_ERROR, item.path, item.message);
157-
158-
if (item.inner) {
159-
// TODO: I am honestly not sure what the correct behavior is
160-
// here. Some items will have within them a tree of other items,
161-
// some of which might contain more info (but it's unclear).
162-
// Do we treat them as their own error or do something else?
163-
queue.push(item.inner);
164-
}
165-
});
166-
167-
queue.shift();
168-
}
152+
err.details.forEach((error) => {
153+
this.createValidationAnnotation(error);
154+
});
169155

170156
return done(null, this.result);
171157
}
@@ -1562,7 +1548,24 @@ class Parser {
15621548

15631549
// Create annotation from swagger-parser error (ZSchema validation)
15641550
createValidationAnnotation(error) {
1565-
this.createAnnotation(annotations.VALIDATION_ERROR, error.path, error.message);
1551+
let message;
1552+
1553+
if (error.code === 'ENUM_MISMATCH') {
1554+
const enumerations = error[ZSchema.schemaSymbol].enum.map((item, index, items) => {
1555+
if (index === items.length - 1) {
1556+
return `or '${item}'`;
1557+
}
1558+
1559+
return `'${item}'`;
1560+
}).join(', ');
1561+
1562+
const value = error.params[0];
1563+
message = `Value must be either ${enumerations} not '${value}'`;
1564+
} else {
1565+
({ message } = error);
1566+
}
1567+
1568+
this.createAnnotation(annotations.VALIDATION_ERROR, error.path, message);
15661569

15671570
if (error.inner) {
15681571
error.inner.forEach((innerError) => {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@
163163
]
164164
}
165165
},
166-
"content": "No enum match for: foo"
166+
"content": "Value must be either 'array', 'boolean', 'integer', 'null', 'number', 'object', or 'string' not 'foo'"
167167
},
168168
{
169169
"element": "annotation",

0 commit comments

Comments
 (0)