forked from raml2html/raml2obj
-
Notifications
You must be signed in to change notification settings - Fork 0
/
consistency-helpers.js
65 lines (54 loc) · 1.7 KB
/
consistency-helpers.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
function _isObject(obj) {
return obj === Object(obj);
}
function makeConsistent(obj, types) {
if (_isObject(obj)) {
if (obj.type) {
if (Array.isArray(obj.type)) {
obj.type = obj.type[0];
}
if (types && types[obj.type]) {
Object.assign(obj, types[obj.type]);
}
}
if (obj.items && types && types[obj.items]) {
obj.items = types[obj.items];
}
if (obj.examples && obj.examples.length) {
obj.examples = obj.examples.map(example => (example.value ? example.value : example));
}
if (obj.structuredExample) {
if (typeof obj.examples === 'undefined') {
obj.examples = [];
}
obj.examples.push(obj.structuredExample.value);
delete obj.example;
delete obj.structuredExample;
}
// The RAML 1.0 spec allows that:
// "A securedBy node containing null as the array component indicates
// the method can be called without applying any security scheme."
if (Array.isArray(obj.securedBy) && obj.securedBy.length === 1 && obj.securedBy[0] === null) {
delete obj.securedBy;
}
// Fix inconsistency between request headers and response headers from raml-1-parser.
// https://github.com/raml-org/raml-js-parser-2/issues/582
if (Array.isArray(obj.headers)) {
obj.headers.forEach((hdr) => {
if (typeof hdr.key === 'undefined' && hdr.name) {
hdr.key = hdr.name;
}
});
}
Object.keys(obj).forEach((key) => {
const value = obj[key];
makeConsistent(value, types);
});
} else if (Array.isArray(obj)) {
obj.forEach((value) => {
makeConsistent(value, types);
});
}
return obj;
}
module.exports = makeConsistent;