|
1 |
| -var InvalidTypeError = require('./errors/invalid-type-error'); |
| 1 | +var convertSchema = require('./converters/schema'); |
2 | 2 |
|
3 | 3 | module.exports = {
|
4 |
| - schema: convertSchema |
| 4 | + schema: convertSchema, |
5 | 5 | };
|
6 | 6 |
|
7 |
| -function convertSchema(schema, options) { |
8 |
| - var structs = options._structs |
9 |
| - , notSupported = options._notSupported |
10 |
| - , i = 0 |
11 |
| - , j = 0 |
12 |
| - , struct = null |
13 |
| - ; |
14 |
| - |
15 |
| - for (i; i < structs.length; i++) { |
16 |
| - struct = structs[i]; |
17 |
| - |
18 |
| - if (Array.isArray(schema[struct])) { |
19 |
| - for (j; j < schema[struct].length; j++) { |
20 |
| - schema[struct][j] = convertSchema(schema[struct][j], options); |
21 |
| - } |
22 |
| - } else if (typeof schema[struct] === 'object') { |
23 |
| - schema[struct] = convertSchema(schema[struct], options); |
24 |
| - } |
25 |
| - } |
26 |
| - |
27 |
| - if (typeof schema.properties === 'object') { |
28 |
| - schema.properties = convertProperties(schema.properties, options); |
29 |
| - |
30 |
| - if (Array.isArray(schema.required)) { |
31 |
| - schema.required = cleanRequired(schema.required, schema.properties); |
32 |
| - |
33 |
| - if (schema.required.length === 0) { |
34 |
| - delete schema.required; |
35 |
| - } |
36 |
| - } |
37 |
| - if (Object.keys(schema.properties).length === 0) { |
38 |
| - delete schema.properties; |
39 |
| - } |
40 |
| - |
41 |
| - } |
42 |
| - |
43 |
| - validateType(schema.type); |
44 |
| - schema = convertTypes(schema); |
45 |
| - schema = convertFormat(schema, options); |
46 |
| - |
47 |
| - if (typeof schema['x-patternProperties'] === 'object' |
48 |
| - && options.supportPatternProperties) { |
49 |
| - schema = convertPatternProperties(schema, options.patternPropertiesHandler); |
50 |
| - } |
51 |
| - |
52 |
| - for (i=0; i < notSupported.length; i++) { |
53 |
| - delete schema[notSupported[i]]; |
54 |
| - } |
55 |
| - |
56 |
| - return schema; |
57 |
| -} |
58 |
| - |
59 |
| -function validateType(type) { |
60 |
| - var validTypes = ['integer', 'number', 'string', 'boolean', 'object', 'array']; |
61 |
| - |
62 |
| - if (validTypes.indexOf(type) < 0 && type !== undefined) { |
63 |
| - throw new InvalidTypeError('Type "' + type + '" is not a valid type'); |
64 |
| - } |
65 |
| -} |
66 |
| - |
67 |
| - |
68 |
| -function convertProperties(properties, options) { |
69 |
| - var key |
70 |
| - , property |
71 |
| - , props = {} |
72 |
| - , removeProp |
73 |
| - ; |
74 |
| - |
75 |
| - for (key in properties) { |
76 |
| - removeProp = false; |
77 |
| - property = properties[key]; |
78 |
| - |
79 |
| - options._removeProps.forEach(function(prop) { |
80 |
| - if (property[prop] === true) { |
81 |
| - removeProp = true; |
82 |
| - } |
83 |
| - }); |
84 |
| - |
85 |
| - if (removeProp) { |
86 |
| - continue; |
87 |
| - } |
88 |
| - |
89 |
| - props[key] = convertSchema(property, options); |
90 |
| - } |
91 |
| - |
92 |
| - return props; |
93 |
| -} |
94 |
| - |
95 |
| -function convertTypes(schema) { |
96 |
| - if (schema.type !== undefined && schema.nullable === true) { |
97 |
| - schema.type = [schema.type, 'null']; |
98 |
| - } |
99 |
| - |
100 |
| - return schema; |
101 |
| -} |
102 |
| - |
103 |
| -function convertFormat(schema, options) { |
104 |
| - var format = schema.format; |
105 |
| - var settings = { |
106 |
| - MIN_INT_32: 0 - Math.pow(2, 31), |
107 |
| - MAX_INT_32: Math.pow(2, 31) - 1, |
108 |
| - MIN_INT_64: 0 - Math.pow(2, 63), |
109 |
| - MAX_INT_64: Math.pow(2, 63) - 1, |
110 |
| - MIN_FLOAT: 0 - Math.pow(2, 128), |
111 |
| - MAX_FLOAT: Math.pow(2, 128) - 1, |
112 |
| - MIN_DOUBLE: 0 - Number.MAX_VALUE, |
113 |
| - MAX_DOUBLE: Number.MAX_VALUE, |
114 |
| - |
115 |
| - // Matches base64 (RFC 4648) |
116 |
| - // Matches `standard` base64 not `base64url`. The specification does not |
117 |
| - // exclude it but current ongoing OpenAPI plans will distinguish btoh. |
118 |
| - BYTE_PATTERN: '^[\\w\\d+\\/=]*$' |
119 |
| - }; |
120 |
| - |
121 |
| - // Valid JSON schema v4 formats |
122 |
| - var FORMATS = ['date-time', 'email', 'hostname', 'ipv4', 'ipv6', 'uri', 'uri-reference']; |
123 |
| - |
124 |
| - if (format === undefined || FORMATS.indexOf(format) !== -1) { |
125 |
| - return schema; |
126 |
| - } |
127 |
| - |
128 |
| - if (format === 'date' && options.dateToDateTime === true) { |
129 |
| - return convertFormatDate(schema); |
130 |
| - } |
131 |
| - |
132 |
| - var formatConverters = { |
133 |
| - int32: convertFormatInt32, |
134 |
| - int64: convertFormatInt64, |
135 |
| - float: convertFormatFloat, |
136 |
| - double: convertFormatDouble, |
137 |
| - byte: convertFormatByte |
138 |
| - }; |
139 |
| - |
140 |
| - var converter = formatConverters[format]; |
141 |
| - |
142 |
| - if (converter === undefined) { return schema; } |
143 |
| - |
144 |
| - return converter(schema, settings); |
145 |
| -} |
146 |
| - |
147 |
| -function convertFormatInt32(schema, settings) { |
148 |
| - schema.minimum = settings.MIN_INT_32; |
149 |
| - schema.maximum = settings.MAX_INT_32; |
150 |
| - return schema; |
151 |
| -} |
152 |
| - |
153 |
| -function convertFormatInt64(schema, settings) { |
154 |
| - schema.minimum = settings.MIN_INT_64; |
155 |
| - schema.maximum = settings.MAX_INT_64; |
156 |
| - return schema; |
157 |
| -} |
158 |
| - |
159 |
| -function convertFormatFloat(schema, settings) { |
160 |
| - schema.minimum = settings.MIN_FLOAT; |
161 |
| - schema.maximum = settings.MAX_FLOAT; |
162 |
| - return schema; |
163 |
| -} |
164 |
| - |
165 |
| -function convertFormatDouble(schema, settings) { |
166 |
| - schema.minimum = settings.MIN_DOUBLE; |
167 |
| - schema.maximum = settings.MAX_DOUBLE; |
168 |
| - return schema; |
169 |
| -} |
170 |
| - |
171 |
| -function convertFormatDate(schema) { |
172 |
| - schema.format = 'date-time'; |
173 |
| - return schema; |
174 |
| -} |
175 |
| - |
176 |
| -function convertFormatByte (schema, settings) { |
177 |
| - schema.pattern = settings.BYTE_PATTERN; |
178 |
| - return schema; |
179 |
| -} |
180 |
| - |
181 |
| -function convertPatternProperties(schema, handler) { |
182 |
| - schema.patternProperties = schema['x-patternProperties']; |
183 |
| - delete schema['x-patternProperties']; |
184 |
| - |
185 |
| - return handler(schema); |
186 |
| -} |
187 |
| - |
188 |
| -function cleanRequired(required, properties) { |
189 |
| - var i = 0; |
190 |
| - |
191 |
| - required = required || []; |
192 |
| - properties = properties || {}; |
193 |
| - |
194 |
| - for (i; i < required.length; i++) { |
195 |
| - if (properties[required[i]] === undefined) { |
196 |
| - required.splice(i, 1); |
197 |
| - } |
198 |
| - } |
199 |
| - |
200 |
| - return required; |
201 |
| -} |
0 commit comments