-
Notifications
You must be signed in to change notification settings - Fork 4
/
jsonschema-src.js
477 lines (392 loc) · 13.7 KB
/
jsonschema-src.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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
/**
* JsonSchema generator
* http://www.jsonschema.net/js/Schema.js
* Here for inspiration; not part of this generator
* This generator used Backbone and BB models
*/
// Namespace.
var JsonSchema = new function() {
StateEnum = {
COMPLETE: 0
}
/* Models.js */
Type = Backbone.Model.extend({
defaults: {
t: undefined
}
});
TypeList = Backbone.Collection.extend({
model: Type,
filterByType: function(aType) {
return this.filter(
function(item) {
return item.get('t') == aType;
});
},
isObject: function() {
return (this.filterByType('object').length > 0);
},
isString: function() {
return (this.filterByType('string').length > 0);
},
isNumber: function() {
return (this.filterByType('number').length > 0);
},
isInteger: function() {
return (this.filterByType('integer').length > 0);
},
isBoolean: function() {
return (this.filterByType('boolean').length > 0);
},
isNull: function() {
return (this.filterByType('null').length > 0);
},
isAny: function() {
return (this.filterByType('any').length > 0);
},
isArray: function() {
return (this.filterByType('array').length > 0);
}
});
/* Util.js */
TypeEnum = {
STRING: "string",
NUMBER: "number",
INTEGER: "integer",
BOOLEAN: "boolean",
OBJECT: "object",
ARRAY: "array",
NULL: "null",
ANY: "any",
UNDEFINED: "undefined"
};
ListTypeEnum = {
PROPERTIES: "properties",
ITEMS: "items"
};
function ProxyRequest(url, result) {
$.ajax({
url: 'proxy.php',
data: {
action: 'ref',
url: url
},
type: 'post',
dataType: 'json',
global: false,
async: false,
success: function(output) {
result['value'] = output;
}
});
}
function RealTypeOf(v) {
if (typeof(v) == TypeEnum.OBJECT) {
if (v === null) {
return TypeEnum.NULL;
}
if (v.constructor == (new Array).constructor) {
return TypeEnum.ARRAY;
}
return TypeEnum.OBJECT;
}
return typeof(v);
}
/* end Util.js */
function inherit(parent, child) {
// Copy simple attributes that have a value, from child into parent.
parent.set(child.simpleAttributesWithVal());
/* Copy all complex attributes from child into parent.
Array will be empty if there are no values. */
parent.addItem(_.toArray(child.get('items')));
parent.addExtension(_.toArray(child.get('extensions')));
parent.addOrReplaceProperties(_.toArray(child.get('properties')));
parent.addTypes(_.toArray(child.get('type')));
}
function Schema4Schema(aJsonObject) {
var schema = new Schema();
var keys = Object.keys(aJsonObject);
var state = undefined;
for (k in keys) {
var attributeKey = keys[k];
var attributeValue = aJsonObject[attributeKey];
var isComplexAttribute = (_.indexOf(schema.complexSchemaKeys(), attributeKey) >= 0);
if (isComplexAttribute) {
state = schema4ComplexAttr(schema, attributeValue, attributeKey);
} else {
state = schema4SimpleAttr(schema, attributeValue, attributeKey);
}
if (StateEnum.COMPLETE == state) {
break;
}
}
return schema;
}
function schema4ComplexAttr(aSchema, aAttributeValue, aAttributeKey) {
if (aAttributeKey == '$ref') {
return refAttr(aSchema, aAttributeValue);
} else if (aAttributeKey == 'extends') {
return extensionAttr(aSchema, aAttributeValue);
} else if (aAttributeKey == 'items') {
return itemsAttr(aSchema, aAttributeValue);
} else if (aAttributeKey == 'type') {
return typesAttr(aSchema, aAttributeValue);
} else if (aAttributeKey == 'properties') {
return propertiesAttr(aSchema, aAttributeValue);
}
}
function refAttr(aSchema, aAttributeValue) {
if (JsonSchema.RESOLVE_REFS) {
var result = new ProxyResult();
ProxyRequest(aAttributeValue, result);
var referencedSchema = Schema4Schema(JSON.parse(result.value));
// $ref replaces everything.
_.extend(aSchema, referencedSchema);
// So we need to stop any further passing of schema attributes.
return StateEnum.COMPLETE;
} else {
schema4SimpleAttr(aSchema, aAttributeValue, '$ref');
}
}
function extensionAttr(aSchema, aAttributeValue) {
var attributeValueType = RealTypeOf(aAttributeValue);
if (attributeValueType == TypeEnum.OBJECT) {
var parentSchema = Schema4Schema(aAttributeValue);
if (JsonSchema.MERGE_EXTS) {
inherit(parentSchema, aSchema);
_.extend(aSchema, parentSchema);
} else {
var sp = new SchemaPair({
schema: parentSchema
});
aSchema.addExtension(sp);
}
} else if (attributeValueType == TypeEnum.ARRAY) {
var nestedKeys = Object.keys(aAttributeValue);
for (l in nestedKeys) {
var nestedAttrKey = nestedKeys[l];
var nestedAttrValue = aAttributeValue[nestedAttrKey];
var nestedSchema = Schema4Schema(nestedAttrValue);
var nestedSchemaPair = new SchemaPair({
schema: nestedSchema
});
if (JsonSchema.MERGE_EXTS) {
inherit(nestedSchema, schema);
return nestedSchema;
} else {
aSchema.addExtension(nestedSchemaPair);
}
}
}
}
function itemsAttr(aSchema, aAttributeValue) {
var attributeValueType = RealTypeOf(aAttributeValue);
if (attributeValueType == TypeEnum.OBJECT) {
var nestedSchemaPair = new SchemaPair({
schema: Schema4Schema(aAttributeValue)
});
aSchema.addItem(nestedSchemaPair);
} else if (attributeValueType == TypeEnum.ARRAY) {
var nestedKeys = Object.keys(aAttributeValue);
for (m in nestedKeys) {
var nestedAttrKey = nestedKeys[m];
var nestedAttrValue = aAttributeValue[nestedAttrKey];
var nestedAttrValueType = RealTypeOf(nestedAttrValue);
var nestedSchema = Schema4Schema(nestedAttrValue);
var nestedSchemaPair = new SchemaPair({
schema: nestedSchema
});
aSchema.addItem(nestedSchemaPair);
}
}
}
function typesAttr(aSchema, aAttributeValue) {
var attributeValueType = RealTypeOf(aAttributeValue);
if (attributeValueType == TypeEnum.ARRAY) {
var nestedKeys = Object.keys(aAttributeValue);
for (n in nestedKeys) {
var nestedAttrKey = nestedKeys[n];
var nestedAttrValue = aAttributeValue[nestedAttrKey];
var type = new Type({
t: nestedAttrValue
});
aSchema.addType(type);
}
} else if (attributeValueType == TypeEnum.STRING) {
var type = new Type({
t: aAttributeValue
});
aSchema.addType(type);
}
}
function propertiesAttr(aSchema, aAttributeValue) {
var nestedKeys = Object.keys(aAttributeValue);
for (l in nestedKeys) {
var nestedPropertyKey = nestedKeys[l];
var nestedPropertyValue = aAttributeValue[nestedPropertyKey];
var nestedPropertyValueType = RealTypeOf(nestedPropertyValue);
var nestedSchema = Schema4Schema(nestedPropertyValue);
var nestedSchemaPair = new SchemaPair({
key: nestedPropertyKey,
schema: nestedSchema
});
aSchema.addOrReplaceProperty(nestedSchemaPair);
}
}
function schema4SimpleAttr(aSchema, aValue, aKey) {
if (aKey == '$schema') {
aSchema.set({
dollarschema: aValue
});
} else if (aKey == '$ref') {
aSchema.set({
dollarref: aValue
});
} else if (aKey == 'required') {
aSchema.set({
required: aValue
});
} else if (aKey == 'title') {
aSchema.set({
title: aValue
});
} else if (aKey == 'name') {
aSchema.set({
name: aValue
});
} else if (aKey == 'id') {
aSchema.set({
schemaid: aValue
});
} else if (aKey == 'description') {
aSchema.set({
description: aValue
});
} else if (aKey == 'minimum') {
aSchema.set({
minimum: aValue
});
} else if (aKey == 'maximum') {
aSchema.set({
maximum: aValue
});
} else if (aKey == 'minitems') {
aSchema.set({
minitems: aValue
});
} else if (aKey == 'maxitems') {
aSchema.set({
maxitems: aValue
});
}
}
function Schema4Object(aJsonObject) {
var objectType = new Type({
t: TypeEnum.OBJECT
});
var schema = new Schema();
schema.addType(objectType);
var keys = Object.keys(aJsonObject);
for (k in keys) {
var propertyKey = keys[k];
var propertyValue = aJsonObject[propertyKey];
var propertyValueType = RealTypeOf(propertyValue);
console.log(propertyKey + " : " + propertyValue + " (" + propertyValueType + ")");
var propertySchema = null;
var propertySchemaPair = null;
if (propertyValueType == TypeEnum.OBJECT) {
propertySchema = Schema4Object(propertyValue);
} else if (propertyValueType == TypeEnum.ARRAY) {
propertySchema = Schema4Array(propertyValue);
} else {
propertySchema = Schema4Value(propertyValue);
}
propertySchemaPair = new SchemaPair({
key: propertyKey,
schema: propertySchema
});
schema.addProperty(propertySchemaPair);
}
return schema;
}
function Schema4Value(aJsonValue) {
var valueType = RealTypeOf(aJsonValue);
var type = new Type({
t: valueType
});
var schema = new Schema();
schema.addType(type);
if (JsonSchema.INCLUDE_DEFS) {
schema.set({
defaultValue: aJsonValue
});
}
return schema;
}
function Schema4Array(aJsonArray) {
var schema = new Schema();
var type = new Type({
t: TypeEnum.ARRAY
});
schema.addType(type);
var keys = Object.keys(aJsonArray);
var existingSchemaItems = new Array();
var itemSchemaPairs = new Array();
var doTupleTyping = false;
var firstKey = true;
for (k in keys) {
var propertyKey = keys[k];
console.log(propertyKey);
var propertyValue = aJsonArray[propertyKey];
var propertyValueType = RealTypeOf(propertyValue);
var itemSchema;
if (propertyValueType == TypeEnum.OBJECT) {
itemSchema = Schema4Object(propertyValue);
} else {
itemSchema = Schema4Value(propertyValue);
}
var itemSchemaPair = new SchemaPair({
schema: itemSchema
});
itemSchemaPairs.push(itemSchemaPair);
var schemaAsJsonString = JSON.stringify(itemSchema.toJSON());
var duplicateSchema = (_.indexOf(existingSchemaItems, schemaAsJsonString) >= 0);
if (!duplicateSchema) {
existingSchemaItems.push(schemaAsJsonString);
}
/*
If more than one unique schema is required for this array, then we need all schemas.
As long as there are no duplicate schemas, then one schema serves all items in the array.
*/
doTupleTyping = (!duplicateSchema && !firstKey);
firstKey = false;
}
if (doTupleTyping) {
schema.addItems(itemSchemaPairs);
} else {
// All items can be represented with same schema, so just use first.
schema.addItem(itemSchemaPairs[0]);
}
return schema;
}
// ---------- Public Objects ---------- //
this.GenerateSchema = function() {
var schemaVersion = 'http://json-schema.org/draft-03/schema';
var jsonObject = null;
var schema = null;
try {
jsonObject = JSON.parse(JsonSchema.INPUT_VALUE);
} catch (err) {
throw (err);
}
if (JsonSchema.INPUT_MODE == 'schema') {
schema = Schema4Schema(jsonObject);
} else {
schema = Schema4Object(jsonObject);
schema.set({
dollarschema: schemaVersion
});
}
return schema;
}
};