-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.js
97 lines (74 loc) · 2.31 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
var deepEqual = require('deep-equal');
var convert = require('./lib/convert');
module.exports = openapiSchemaToJsonSchema;
module.exports.fromSchema = openapiSchemaToJsonSchema;
module.exports.fromParameter = openapiParameterToJsonSchema;
function openapiSchemaToJsonSchema(schema, options) {
options = resolveOptions(options);
if (options.cloneSchema) {
schema = JSON.parse(JSON.stringify(schema));
}
var jsonSchema = convert.fromSchema(schema, options);
return jsonSchema;
}
function openapiParameterToJsonSchema(parameter, options) {
options = resolveOptions(options);
if (options.cloneSchema) {
parameter = JSON.parse(JSON.stringify(parameter));
}
var jsonSchema = convert.fromParameter(parameter, options);
return jsonSchema;
}
function resolveOptions(options) {
var notSupported = [
'nullable', 'discriminator', 'readOnly',
'writeOnly', 'xml', 'externalDocs',
'example', 'deprecated'
];
options = options || {};
options.dateToDateTime = options.dateToDateTime || false;
options.cloneSchema = options.cloneSchema == false ? false : true;
options.supportPatternProperties = options.supportPatternProperties || false;
options.keepNotSupported = options.keepNotSupported || [];
if (typeof options.patternPropertiesHandler !== 'function') {
options.patternPropertiesHandler = patternPropertiesHandler;
}
options._removeProps = [];
if (options.removeReadOnly === true) {
options._removeProps.push('readOnly');
}
if (options.removeWriteOnly === true) {
options._removeProps.push('writeOnly');
}
options._structs = ['allOf', 'anyOf', 'oneOf', 'not', 'items', 'additionalProperties'];
options._notSupported = resolveNotSupported(notSupported, options.keepNotSupported);
return options;
}
function patternPropertiesHandler(schema) {
var pattern
, patternsObj = schema.patternProperties
, additProps = schema.additionalProperties
;
if (typeof additProps !== 'object') {
return schema;
}
for (pattern in patternsObj) {
if (deepEqual(patternsObj[pattern], additProps)) {
schema.additionalProperties = false;
break;
}
}
return schema;
}
function resolveNotSupported(notSupported, toRetain) {
var i = 0
, index
;
for (i; i < toRetain.length; i++) {
index = notSupported.indexOf(toRetain[i]);
if (index >= 0) {
notSupported.splice(index, 1);
}
}
return notSupported;
}