-
Notifications
You must be signed in to change notification settings - Fork 0
/
transform.js
197 lines (150 loc) · 4.06 KB
/
transform.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/**
* Transform Swagger specs to JSON Schema.
*/
'use strict';
/**
* Module dependencies.
*/
var lodash = require('lodash');
var constants = require('./constants');
/**
* Traverse object.
*/
function traverse(value, cbs) {
cbs = cbs || {};
if (Array.isArray(value)) {
if (cbs.beforeArray) value = cbs.beforeArray(value, cbs);
value = value.map(function(v) {
return traverse(v, cbs);
});
if (cbs.afterArray) value = cbs.afterArray(value, cbs);
} else if (typeof value === 'object') {
if (cbs.beforeObject) value = cbs.beforeObject(value, cbs);
Object.keys(value).forEach(function(key) {
value[key] = traverse(value[key], cbs);
});
if (cbs.afterObject) value = cbs.afterObject(value, cbs);
}
return value;
}
/**
* Transform Swagger spec.
*/
function transform(value, options) {
options = options || {};
return traverse(value, {
beforeObject: function(v) {
if (options.removeFormats &&
typeof v.format === 'string' &&
typeof v.type === 'string' &&
v.type !== 'string') {
delete v.format;
}
return v;
},
afterObject: function(v) {
if (options.convertRefs &&
typeof v.type === 'string' &&
constants.notModel.indexOf(v.type) < 0) {
return { $ref: v.type };
}
return v;
},
});
}
/**
* Transform Swagger parameters into JSON Schema object by paramType.
*/
function parameters(spec, paramType) {
spec = lodash.cloneDeep(spec);
var schema = {
type: 'object',
properties: {},
required: [],
};
spec.parameters.forEach(function(parameter) {
// only get rules for current paramType
if (parameter.paramType !== paramType) return;
// create current property
var property = schema.properties[parameter.name] = {
type: parameter.type,
};
switch (parameter.type) {
case 'integer':
property.type = parameter.type;
['minimum', 'maximum'].forEach(function(key) {
if (parameter.hasOwnProperty(key)) {
property[key] = parseInt(parameter[key], 10);
}
});
break;
case 'number':
property.type = parameter.type;
break;
case 'string':
property.type = parameter.type;
if (parameter.hasOwnProperty('minimum')) {
property.minLength = parseInt(parameter.minimum, 10);
}
if (parameter.hasOwnProperty('maximum')) {
property.maxLength = parseInt(parameter.maximum, 10);
}
break;
case 'boolean':
property.type = parameter.type;
break;
case 'array':
property.type = parameter.type;
property.items = parameter.items;
if (parameter.uniqueItems) property.uniqueItems = true;
break;
default:
if (paramType === 'body') {
property.$ref = parameter.type;
lodash.keys(property).forEach(function(key) {
if (key !== '$ref') delete property[key];
});
} else {
throw new Error('unknown type: ' + parameter.type);
}
}
if (Array.isArray(parameter.enum)) {
property.enum = parameter.enum;
}
var hasDefault = parameter.hasOwnProperty('defaultValue');
if (hasDefault) {
property.default = parameter.defaultValue;
}
// path rules are implicitly required
if (!hasDefault && (parameter.required || paramType === 'path')) {
schema.required.push(parameter.name);
}
// convert to items if allowMultiple
if (parameter.allowMultiple &&
constants.allowMultiple.indexOf(parameter.paramType) >= 0) {
schema.properties[parameter.name] = {
type: 'array',
items: property,
};
}
});
if (!schema.required.length) delete schema.required;
if (lodash.isEmpty(schema.properties)) return;
return schema;
}
/**
* Convert Swagger model to JSON Schema.
*/
function model(spec) {
var schema = lodash.cloneDeep(spec);
delete schema.id;
schema = transform(schema, {
removeFormats: true,
});
return schema;
}
/**
* Expose transform.
*/
exports.model = model;
exports.parameters = parameters;