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

Commit f1d81c3

Browse files
committed
fix(oas2): add sourcemaps to some swagger validation errors
1 parent c6dea60 commit f1d81c3

File tree

4 files changed

+192
-0
lines changed

4 files changed

+192
-0
lines changed

packages/openapi2-parser/lib/parser.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,15 @@ class Parser {
174174
annotation.attributes.set('sourceMap', [
175175
new SourceMap([location]),
176176
]);
177+
} else {
178+
// Some validation errors contain `in '/some/path'`,
179+
// we can extract path to get source map
180+
const matchesPath = message.match(/in '\/(.*?)'/);
181+
182+
if (matchesPath) {
183+
const path = matchesPath[1].split('/');
184+
this.createSourceMap(annotation, path, true);
185+
}
177186
}
178187

179188
return done(null, this.result);
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
{
2+
"element": "parseResult",
3+
"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": 6
55+
},
56+
"column": {
57+
"element": "number",
58+
"content": 2
59+
}
60+
},
61+
"content": 98
62+
},
63+
{
64+
"element": "number",
65+
"attributes": {
66+
"line": {
67+
"element": "number",
68+
"content": 9
69+
},
70+
"column": {
71+
"element": "number",
72+
"content": 0
73+
}
74+
},
75+
"content": 43
76+
}
77+
]
78+
}
79+
]
80+
}
81+
]
82+
}
83+
},
84+
"content": "Validation failed. Property 'foo' listed as required but does not exist in '/definitions/test'"
85+
}
86+
]
87+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
{
2+
"element": "parseResult",
3+
"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": 6
55+
},
56+
"column": {
57+
"element": "number",
58+
"content": 2
59+
}
60+
},
61+
"content": 98
62+
},
63+
{
64+
"element": "number",
65+
"attributes": {
66+
"line": {
67+
"element": "number",
68+
"content": 9
69+
},
70+
"column": {
71+
"element": "number",
72+
"content": 0
73+
}
74+
},
75+
"content": 43
76+
}
77+
]
78+
}
79+
]
80+
}
81+
]
82+
}
83+
},
84+
"content": "Validation failed. Property 'foo' listed as required but does not exist in '/definitions/test'"
85+
}
86+
]
87+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
swagger: '2.0'
2+
info:
3+
title: Swagger Validation Errors
4+
version: 1.0.0
5+
paths: {}
6+
definitions:
7+
test:
8+
type: object
9+
required: [foo]

0 commit comments

Comments
 (0)