-
Notifications
You must be signed in to change notification settings - Fork 36
/
treema-utils.js
322 lines (321 loc) · 9.19 KB
/
treema-utils.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
var TreemaUtils;
TreemaUtils = (function() {
var utils;
utils = {};
utils.populateDefaults = function(rootData, rootSchema, tv4) {
var _this = this;
if (rootSchema["default"] && !rootData) {
rootData = this.cloneDeep(rootSchema["default"]);
}
this.walk(rootData, rootSchema, tv4, function(path, data, schema) {
var def, key, value, _results;
def = schema["default"];
if (!(_this.type(def) === 'object' && _this.type(data) === 'object')) {
return;
}
_results = [];
for (key in def) {
value = def[key];
if (data[key] === void 0) {
_results.push(data[key] = _this.cloneDeep(value));
} else {
_results.push(void 0);
}
}
return _results;
});
return rootData;
};
utils.populateRequireds = function(rootData, rootSchema, tv4) {
var _this = this;
if (rootData == null) {
rootData = {};
}
this.walk(rootData, rootSchema, tv4, function(path, data, schema) {
var childSchema, key, schemaDefault, type, workingSchema, _i, _len, _ref, _ref1, _results;
if (!(schema.required && _this.type(data) === 'object')) {
return;
}
_ref = schema.required;
_results = [];
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
key = _ref[_i];
if (data[key] != null) {
continue;
}
if (schemaDefault = (_ref1 = schema["default"]) != null ? _ref1[key] : void 0) {
_results.push(data[key] = _this.cloneDeep(schemaDefault));
} else {
childSchema = _this.getChildSchema(key, schema);
workingSchema = _this.buildWorkingSchemas(childSchema, tv4)[0];
schemaDefault = workingSchema["default"];
if (schemaDefault != null) {
_results.push(data[key] = _this.cloneDeep(schemaDefault));
} else {
type = workingSchema.type;
if (_this.type(type) === 'array') {
type = type[0];
}
if (!type) {
type = 'string';
}
_results.push(data[key] = _this.defaultForType(type));
}
}
}
return _results;
});
return rootData;
};
utils.walk = function(data, schema, tv4, callback, path) {
var dataType, f, key, value, workingSchema, workingSchemas, _i, _len, _results, _results1,
_this = this;
if (path == null) {
path = '';
}
if (!tv4) {
tv4 = this.getGlobalTv4().freshApi();
tv4.addSchema('#', schema);
if (schema.id) {
tv4.addSchema(schema.id, schema);
}
}
workingSchemas = this.buildWorkingSchemas(schema, tv4);
workingSchema = this.chooseWorkingSchema(data, workingSchemas, tv4);
callback(path, data, workingSchema);
dataType = this.type(data);
if (dataType === 'array' || dataType === 'object') {
f = function(key, value) {
var childPath, childSchema;
value = data[key];
childPath = path.slice();
if (childPath) {
childPath += '.';
}
childPath += key;
childSchema = _this.getChildSchema(key, workingSchema);
return _this.walk(value, childSchema, tv4, callback, childPath);
};
if (dataType === 'array') {
_results = [];
for (key = _i = 0, _len = data.length; _i < _len; key = ++_i) {
value = data[key];
_results.push(f(key, value));
}
return _results;
} else {
_results1 = [];
for (key in data) {
value = data[key];
_results1.push(f(key, value));
}
return _results1;
}
}
};
utils.getChildSchema = function(key, schema) {
var childKey, childSchema, index, _ref, _ref1;
if (this.type(key) === 'string') {
_ref = schema.properties;
for (childKey in _ref) {
childSchema = _ref[childKey];
if (childKey === key) {
return childSchema;
}
}
_ref1 = schema.patternProperties;
for (childKey in _ref1) {
childSchema = _ref1[childKey];
if (key.match(new RegExp(childKey))) {
return childSchema;
}
}
if (typeof schema.additionalProperties === 'object') {
return schema.additionalProperties;
}
}
if (this.type(key) === 'number') {
index = key;
if (schema.items) {
if (Array.isArray(schema.items)) {
if (index < schema.items.length) {
return schema.items[index];
} else if (schema.additionalItems) {
return schema.additionalItems;
}
} else if (schema.items) {
return schema.items;
}
}
}
return {};
};
utils.buildWorkingSchemas = function(schema, tv4) {
var allOf, anyOf, baseSchema, newBase, oneOf, singularSchema, singularSchemas, workingSchemas, _i, _j, _len, _len1;
if (schema == null) {
schema = {};
}
baseSchema = this.resolveReference(schema, tv4);
if (!(schema.allOf || schema.anyOf || schema.oneOf)) {
return [schema];
}
baseSchema = this.cloneSchema(baseSchema);
allOf = baseSchema.allOf;
anyOf = baseSchema.anyOf;
oneOf = baseSchema.oneOf;
if (baseSchema.allOf != null) {
delete baseSchema.allOf;
}
if (baseSchema.anyOf != null) {
delete baseSchema.anyOf;
}
if (baseSchema.oneOf != null) {
delete baseSchema.oneOf;
}
if (allOf != null) {
for (_i = 0, _len = allOf.length; _i < _len; _i++) {
schema = allOf[_i];
this.combineSchemas(baseSchema, this.resolveReference(schema, tv4));
}
}
workingSchemas = [];
singularSchemas = [];
if (anyOf != null) {
singularSchemas = singularSchemas.concat(anyOf);
}
if (oneOf != null) {
singularSchemas = singularSchemas.concat(oneOf);
}
for (_j = 0, _len1 = singularSchemas.length; _j < _len1; _j++) {
singularSchema = singularSchemas[_j];
singularSchema = this.resolveReference(singularSchema, tv4);
newBase = this.cloneSchema(baseSchema);
this.combineSchemas(newBase, singularSchema);
workingSchemas.push(newBase);
}
if (workingSchemas.length === 0) {
workingSchemas = [baseSchema];
}
return workingSchemas;
};
utils.chooseWorkingSchema = function(data, workingSchemas, tv4) {
var result, schema, _i, _len;
if (workingSchemas.length === 1) {
return workingSchemas[0];
}
if (tv4 == null) {
tv4 = this.getGlobalTv4();
}
for (_i = 0, _len = workingSchemas.length; _i < _len; _i++) {
schema = workingSchemas[_i];
result = tv4.validateMultiple(data, schema);
if (result.valid) {
return schema;
}
}
return workingSchemas[0];
};
utils.resolveReference = function(schema, tv4, scrubTitle) {
var resolved;
if (scrubTitle == null) {
scrubTitle = false;
}
if (schema.$ref == null) {
return schema;
}
if (tv4 == null) {
tv4 = this.getGlobalTv4();
}
resolved = tv4.getSchema(schema.$ref);
if (!resolved) {
console.warn('could not resolve reference', schema.$ref, tv4.getMissingUris());
}
if (resolved == null) {
resolved = {};
}
if (scrubTitle && (resolved.title != null)) {
delete resolved.title;
}
return resolved;
};
utils.getGlobalTv4 = function() {
if (typeof window !== 'undefined') {
return window.tv4;
}
if (typeof global !== 'undefined') {
return global.tv4;
}
if (typeof tv4 !== 'undefined') {
return tv4;
}
};
utils.cloneSchema = function(schema) {
var clone, key, value;
clone = {};
for (key in schema) {
value = schema[key];
clone[key] = value;
}
return clone;
};
utils.combineSchemas = function(schema1, schema2) {
var key, value;
for (key in schema2) {
value = schema2[key];
schema1[key] = value;
}
return schema1;
};
utils.cloneDeep = function(data) {
var clone, key, type, value;
clone = data;
type = this.type(data);
if (type === 'object') {
clone = {};
}
if (type === 'array') {
clone = [];
}
if (type === 'object' || type === 'array') {
for (key in data) {
value = data[key];
clone[key] = this.cloneDeep(value);
}
}
return clone;
};
utils.type = (function() {
var classToType, name, _i, _len, _ref;
classToType = {};
_ref = "Boolean Number String Function Array Date RegExp Undefined Null".split(" ");
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
name = _ref[_i];
classToType["[object " + name + "]"] = name.toLowerCase();
}
return function(obj) {
var strType;
strType = Object.prototype.toString.call(obj);
return classToType[strType] || "object";
};
})();
utils.defaultForType = function(type) {
return {
string: '',
number: 0,
"null": null,
object: {},
integer: 0,
boolean: false,
array: []
}[type];
};
if (typeof TreemaNode !== 'undefined') {
return TreemaNode.utils = utils;
} else if (typeof module !== 'undefined' && module.exports) {
return module.exports = utils;
} else {
return utils;
}
})();
;
//# sourceMappingURL=treema-utils.js.map