-
Notifications
You must be signed in to change notification settings - Fork 0
/
schemafy.js
85 lines (83 loc) · 2 KB
/
schemafy.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
/**
* JSON schema generator.
*/
function union(schemas) {
if (schemas.length == 0) {
return null;
}
var type = schemas[0].type;
var keys = {};
var items = [];
for (var i = 0; i < schemas.length; ++i) {
var schema = schemas[i];
if (schema.type != type) {
return null;
}
if (schema.type === 'array') {
items.push(schema.items);
} else if (schema.type === 'object') {
for (var key in schema.properties) {
if (keys[key] == null)
keys[key] = { schemas: [],
count: 0 };
keys[key].schemas.push(schema.properties[key]);
++keys[key].count;
}
}
}
if (type === 'array') {
var schema = { type: 'array' };
var itemsSchema = union(items);
schema.items = itemsSchema;
return schema;
} else if (type === 'object') {
var schema = { type: 'object',
properties: {},
required: [] };
for (var key in keys) {
var keySchema = union(keys[key].schemas);
schema.properties[key] = keySchema;
if (keys[key].count == schemas.length)
schema.required.push(key);
}
return schema;
} else {
return schemas[0];
}
}
function schemafy(json) {
var schema = {};
if (json === null) {
schema.type = "null";
} else if (Array.isArray(json)) {
schema.type = "array";
var items = []
for (var i = 0; i < json.length; ++i)
items.push(schemafy(json[i]));
schema.items = union(items);
} else {
var type = typeof json;
switch (type) {
case 'boolean':
schema.type = 'boolean';
break;
case 'number':
schema.type = 'number';
break;
case 'object':
schema.type = 'object';
schema.properties = {};
schema.required = [];
for (var key in json) {
var val = json[key];
schema.properties[key] = schemafy(val);
schema.required.push(key);
}
break;
case 'string':
schema.type = 'string';
break;
}
}
return schema;
}