|
1 | 1 | var deepEqual = require('deep-equal'); |
| 2 | +var convert = require('./lib/convert'); |
2 | 3 |
|
3 | | -module.exports = convert; |
| 4 | +module.exports = openapiSchemaToJsonSchema; |
4 | 5 |
|
5 | | -function InvalidTypeError(message) { |
6 | | - this.name = 'InvalidTypeError'; |
7 | | - this.message = message; |
8 | | -} |
9 | | - |
10 | | -InvalidTypeError.prototype = new Error(); |
11 | | - |
12 | | -function convert(schema, options) { |
| 6 | +function openapiSchemaToJsonSchema(schema, options) { |
13 | 7 | var notSupported = [ |
14 | 8 | 'nullable', 'discriminator', 'readOnly', |
15 | 9 | 'writeOnly', 'xml', 'externalDocs', |
@@ -43,194 +37,12 @@ function convert(schema, options) { |
43 | 37 | schema = JSON.parse(JSON.stringify(schema)); |
44 | 38 | } |
45 | 39 |
|
46 | | - schema = convertSchema(schema, options); |
| 40 | + schema = convert.schema(schema, options); |
47 | 41 | schema['$schema'] = 'http://json-schema.org/draft-04/schema#'; |
48 | 42 |
|
49 | 43 | return schema; |
50 | 44 | } |
51 | 45 |
|
52 | | -function convertSchema(schema, options) { |
53 | | - var structs = options._structs |
54 | | - , notSupported = options._notSupported |
55 | | - , i = 0 |
56 | | - , j = 0 |
57 | | - , struct = null |
58 | | - ; |
59 | | - |
60 | | - for (i; i < structs.length; i++) { |
61 | | - struct = structs[i]; |
62 | | - |
63 | | - if (Array.isArray(schema[struct])) { |
64 | | - for (j; j < schema[struct].length; j++) { |
65 | | - schema[struct][j] = convertSchema(schema[struct][j], options); |
66 | | - } |
67 | | - } else if (typeof schema[struct] === 'object') { |
68 | | - schema[struct] = convertSchema(schema[struct], options); |
69 | | - } |
70 | | - } |
71 | | - |
72 | | - if (typeof schema.properties === 'object') { |
73 | | - schema.properties = convertProperties(schema.properties, options); |
74 | | - |
75 | | - if (Array.isArray(schema.required)) { |
76 | | - schema.required = cleanRequired(schema.required, schema.properties); |
77 | | - |
78 | | - if (schema.required.length === 0) { |
79 | | - delete schema.required; |
80 | | - } |
81 | | - } |
82 | | - if (Object.keys(schema.properties).length === 0) { |
83 | | - delete schema.properties; |
84 | | - } |
85 | | - |
86 | | - } |
87 | | - |
88 | | - validateType(schema.type); |
89 | | - schema = convertTypes(schema); |
90 | | - schema = convertFormat(schema, options); |
91 | | - |
92 | | - if (typeof schema['x-patternProperties'] === 'object' |
93 | | - && options.supportPatternProperties) { |
94 | | - schema = convertPatternProperties(schema, options.patternPropertiesHandler); |
95 | | - } |
96 | | - |
97 | | - for (i=0; i < notSupported.length; i++) { |
98 | | - delete schema[notSupported[i]]; |
99 | | - } |
100 | | - |
101 | | - return schema; |
102 | | -} |
103 | | - |
104 | | -function convertProperties(properties, options) { |
105 | | - var key |
106 | | - , property |
107 | | - , props = {} |
108 | | - , removeProp |
109 | | - ; |
110 | | - |
111 | | - for (key in properties) { |
112 | | - removeProp = false; |
113 | | - property = properties[key]; |
114 | | - |
115 | | - options._removeProps.forEach(function(prop) { |
116 | | - if (property[prop] === true) { |
117 | | - removeProp = true; |
118 | | - } |
119 | | - }); |
120 | | - |
121 | | - if (removeProp) { |
122 | | - continue; |
123 | | - } |
124 | | - |
125 | | - props[key] = convertSchema(property, options); |
126 | | - } |
127 | | - |
128 | | - return props; |
129 | | -} |
130 | | - |
131 | | -function validateType(type) { |
132 | | - var validTypes = ['integer', 'number', 'string', 'boolean', 'object', 'array']; |
133 | | - |
134 | | - if (validTypes.indexOf(type) < 0 && type !== undefined) { |
135 | | - throw new InvalidTypeError('Type "' + type + '" is not a valid type'); |
136 | | - } |
137 | | -} |
138 | | - |
139 | | -function convertTypes(schema) { |
140 | | - if (schema.type !== undefined && schema.nullable === true) { |
141 | | - schema.type = [schema.type, 'null']; |
142 | | - } |
143 | | - |
144 | | - return schema; |
145 | | -} |
146 | | - |
147 | | -function convertFormat(schema, options) { |
148 | | - var format = schema.format; |
149 | | - |
150 | | - if (format === undefined || FORMATS.indexOf(format) !== -1) { |
151 | | - return schema; |
152 | | - } |
153 | | - |
154 | | - var converter = formatConverters[format]; |
155 | | - |
156 | | - if (converter === undefined) { return schema; } |
157 | | - |
158 | | - return converter(schema, options); |
159 | | -} |
160 | | - |
161 | | -// Valid JSON schema v4 formats |
162 | | -var FORMATS = ['date-time', 'email', 'hostname', 'ipv4', 'ipv6', 'uri', 'uri-reference']; |
163 | | - |
164 | | -function convertFormatInt32 (schema) { |
165 | | - schema.minimum = MIN_INT_32; |
166 | | - schema.maximum = MAX_INT_32; |
167 | | - return schema; |
168 | | -} |
169 | | - |
170 | | -var MIN_INT_32 = 0 - Math.pow(2, 31); |
171 | | -var MAX_INT_32 = Math.pow(2, 31) - 1; |
172 | | - |
173 | | -function convertFormatInt64 (schema) { |
174 | | - schema.minimum = MIN_INT_64; |
175 | | - schema.maximum = MAX_INT_64; |
176 | | - return schema; |
177 | | -} |
178 | | - |
179 | | -var MIN_INT_64 = 0 - Math.pow(2, 63); |
180 | | -var MAX_INT_64 = Math.pow(2, 63) - 1; |
181 | | - |
182 | | -function convertFormatFloat (schema) { |
183 | | - schema.minimum = MIN_FLOAT; |
184 | | - schema.maximum = MAX_FLOAT; |
185 | | - return schema; |
186 | | -} |
187 | | - |
188 | | -var MIN_FLOAT = 0 - Math.pow(2, 128); |
189 | | -var MAX_FLOAT = Math.pow(2, 128) - 1; |
190 | | - |
191 | | -function convertFormatDouble (schema) { |
192 | | - schema.minimum = MIN_DOUBLE; |
193 | | - schema.maximum = MAX_DOUBLE; |
194 | | - return schema; |
195 | | -} |
196 | | - |
197 | | -var MIN_DOUBLE = 0 - Number.MAX_VALUE; |
198 | | -var MAX_DOUBLE = Number.MAX_VALUE; |
199 | | - |
200 | | -function convertFormatDate (schema, options) { |
201 | | - if (options.dateToDateTime === true) { |
202 | | - schema.format = 'date-time'; |
203 | | - } |
204 | | - |
205 | | - return schema; |
206 | | -} |
207 | | - |
208 | | -function convertFormatByte (schema) { |
209 | | - schema.pattern = BYTE_PATTERN; |
210 | | - return schema; |
211 | | -} |
212 | | - |
213 | | -// Matches base64 (RFC 4648) |
214 | | -// Matches `standard` base64 not `base64url`. The specification does not |
215 | | -// exclude it but current ongoing OpenAPI plans will distinguish btoh. |
216 | | -var BYTE_PATTERN = '^[\\w\\d+\\/=]*$'; |
217 | | - |
218 | | -var formatConverters = { |
219 | | - int32: convertFormatInt32, |
220 | | - int64: convertFormatInt64, |
221 | | - float: convertFormatFloat, |
222 | | - double: convertFormatDouble, |
223 | | - date: convertFormatDate, |
224 | | - byte: convertFormatByte |
225 | | -}; |
226 | | - |
227 | | -function convertPatternProperties(schema, handler) { |
228 | | - schema.patternProperties = schema['x-patternProperties']; |
229 | | - delete schema['x-patternProperties']; |
230 | | - |
231 | | - return handler(schema); |
232 | | -} |
233 | | - |
234 | 46 | function patternPropertiesHandler(schema) { |
235 | 47 | var pattern |
236 | 48 | , patternsObj = schema.patternProperties |
@@ -267,17 +79,3 @@ function resolveNotSupported(notSupported, toRetain) { |
267 | 79 | return notSupported; |
268 | 80 | } |
269 | 81 |
|
270 | | -function cleanRequired(required, properties) { |
271 | | - var i = 0; |
272 | | - |
273 | | - required = required || []; |
274 | | - properties = properties || {}; |
275 | | - |
276 | | - for (i; i < required.length; i++) { |
277 | | - if (properties[required[i]] === undefined) { |
278 | | - required.splice(i, 1); |
279 | | - } |
280 | | - } |
281 | | - |
282 | | - return required; |
283 | | -} |
0 commit comments