-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathdefaults.js
205 lines (162 loc) · 4.67 KB
/
defaults.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
(function(root, factory) {
'use strict';
if (typeof require === 'function' && typeof exports === 'object' && typeof module === 'object') {
// CommonJS
module.exports = factory();
} else if (typeof define === 'function' && define.amd) {
// AMD
define('json-schema-defaults', [], function() {
return factory();
});
} else {
// global with noConflict
var jsonSchemaDefaults = root.jsonSchemaDefaults;
root.jsonSchemaDefaults = factory();
root.jsonSchemaDefaults.noConflict = function() {
var defaults = root.jsonSchemaDefaults;
root.jsonSchemaDefaults = jsonSchemaDefaults;
return defaults;
};
}
}(this, function() {
'use strict';
/**
* check whether item is plain object
* @param {*} item
* @return {Boolean}
*/
var isObject = function (item) {
return typeof item === 'object' && item.toString() === {}.toString();
};
/**
* deep object clone
*
* @param {Object} source
* @return {Object}
*/
var clone = function (source) {
var target = {};
for (var key in source) {
if (source.hasOwnProperty(key)) {
if (isObject(source[key])) {
target[key] = clone(source[key]);
} else {
target[key] = source[key];
}
}
}
return target;
};
/**
* returns a result of deep merge of two objects
*
* @param {Object} target
* @param {Object} source
* @return {Object}
*/
var merge = function (target, source) {
target = clone(target);
for (var key in source) {
if (source.hasOwnProperty(key)) {
if (isObject(target[key]) && isObject(source[key])) {
target[key] = merge(target[key], source[key]);
} else {
target[key] = source[key];
}
}
}
return target;
};
/**
* get object by reference. works only with local references that points on
* definitions object
*
* @param {String} path
* @param {Object} definitions
* @return {Object}
*/
var getLocalRef = function (path, definitions) {
path = path.replace(/^#\/definitions\//, '').split('/');
var find = function (path, root) {
var key = path.shift();
if (!root[key]) {
return {};
} else if(!path.length) {
return root[key];
} else {
return find(path, root[key]);
}
};
var result = find(path, definitions);
if (!isObject(result)) {
return result;
}
return clone(result);
};
/**
* merge list of objects from allOf properties
* if some of objects contains $ref field extracts this reference and merge it
*
* @param {Array} allOfList
* @param {Object} definitions
* @return {Object}
*/
var mergeAllOf = function (allOfList, definitions) {
var length = allOfList.length,
index = -1,
result = {};
while (++index < length) {
var item = allOfList[index];
item = (typeof item['$ref'] !== 'undefined') ? getLocalRef(item['$ref'], definitions) : item;
result = merge(result, item);
}
return result;
};
/**
* returns a object that built with default values from json schema
*
* @param {Object} schema
* @param {Object} definitions
* @return {Object}
*/
var defaults = function(schema, definitions) {
if (typeof schema['default'] !== 'undefined') {
return schema['default'];
} else if (typeof schema['allOf'] !== 'undefined') {
var mergedItem = mergeAllOf(schema['allOf'], definitions);
return defaults(mergedItem, definitions);
} else if (typeof schema['$ref'] !== 'undefined') {
var reference = getLocalRef(schema['$ref'], definitions);
return defaults(reference, definitions);
} else if (schema.type === 'object') {
if (!schema.properties) { return {}; }
for (var key in schema.properties) {
if (schema.properties.hasOwnProperty(key)) {
schema.properties[key] = defaults(schema.properties[key], definitions);
if (typeof schema.properties[key] === 'undefined') {
delete schema.properties[key];
}
}
}
return schema.properties;
} else if (schema.type === 'array') {
if (!schema.items) { return []; }
return [defaults(schema.items, definitions)];
}
};
/**
* main function
*
* @param {Object} schema
* @param {Object|undefined} definitions
* @return {Object}
*/
return function (schema, definitions) {
if (typeof definitions === 'undefined') {
definitions = schema.definitions || {};
} else if (isObject(schema.definitions)) {
definitions = merge(definitions, schema.definitions);
}
return defaults(clone(schema), definitions);
};
}));