This repository has been archived by the owner on May 5, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
117 lines (93 loc) · 2.23 KB
/
index.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
module.exports = convert;
function convert(parameters) {
var parametersSchema = {};
var bodySchema = getBodySchema(parameters);
var formDataSchema = getSchema(parameters, 'formData');
var headerSchema = getSchema(parameters, 'header');
var pathSchema = getSchema(parameters, 'path');
var querySchema = getSchema(parameters, 'query');
if (bodySchema) {
parametersSchema.body = bodySchema;
}
if (formDataSchema) {
parametersSchema.formData = formDataSchema;
}
if (headerSchema) {
parametersSchema.headers = headerSchema;
}
if (pathSchema) {
parametersSchema.path = pathSchema;
}
if (querySchema) {
parametersSchema.query = querySchema;
}
return parametersSchema;
}
var VALIDATION_KEYWORDS = [
'additionalItems',
'default',
'description',
'enum',
'exclusiveMaximum',
'exclusiveMinimum',
'format',
'items',
'maxItems',
'maxLength',
'maximum',
'minItems',
'minLength',
'minimum',
'multipleOf',
'pattern',
'title',
'type',
'uniqueItems'
];
function copyValidationKeywords(src, dst) {
if (src && dst) {
for (var i = 0, keys = Object.keys(src), len = keys.length; i < len; i++) {
var keyword = keys[i];
if (VALIDATION_KEYWORDS.indexOf(keyword) > -1 || keyword.slice(0,2) === 'x-') {
dst[keyword] = src[keyword];
}
}
}
}
function getBodySchema(parameters) {
var bodySchema = parameters.filter(function(param) {
return param.in === 'body' && param.schema;
})[0];
if (bodySchema) {
bodySchema = bodySchema.schema;
}
return bodySchema;
}
function getSchema(parameters, type) {
var params = parameters.filter(byIn(type));
var schema;
if (params.length) {
schema = {properties: {}};
params.forEach(function(param) {
var paramSchema = {};
schema.properties[param.name] = paramSchema;
copyValidationKeywords(param, paramSchema);
});
schema.required = getRequiredParams(params);
}
return schema;
}
function getRequiredParams(parameters) {
return parameters.filter(byRequired).map(toName);
}
function byIn(str) {
return function(param) {
return param.in === str;
};
}
function byRequired(param) {
return !!param.required;
}
function toName(param) {
return param.name;
}