Skip to content

Commit dace43b

Browse files
authored
Merge pull request #21 from mikunn/initial-modulirization
Initial modulirization
2 parents 88b2c19 + 1135303 commit dace43b

File tree

5 files changed

+218
-207
lines changed

5 files changed

+218
-207
lines changed

index.js

Lines changed: 4 additions & 206 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,9 @@
11
var deepEqual = require('deep-equal');
2+
var convert = require('./lib/convert');
23

3-
module.exports = convert;
4+
module.exports = openapiSchemaToJsonSchema;
45

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) {
137
var notSupported = [
148
'nullable', 'discriminator', 'readOnly',
159
'writeOnly', 'xml', 'externalDocs',
@@ -43,194 +37,12 @@ function convert(schema, options) {
4337
schema = JSON.parse(JSON.stringify(schema));
4438
}
4539

46-
schema = convertSchema(schema, options);
40+
schema = convert.schema(schema, options);
4741
schema['$schema'] = 'http://json-schema.org/draft-04/schema#';
4842

4943
return schema;
5044
}
5145

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-
23446
function patternPropertiesHandler(schema) {
23547
var pattern
23648
, patternsObj = schema.patternProperties
@@ -267,17 +79,3 @@ function resolveNotSupported(notSupported, toRetain) {
26779
return notSupported;
26880
}
26981

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-
}

lib/convert.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
var convertSchema = require('./converters/schema');
2+
3+
module.exports = {
4+
schema: convertSchema,
5+
};
6+

0 commit comments

Comments
 (0)