-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson-to-schema.js
162 lines (120 loc) · 3.96 KB
/
json-to-schema.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
var availableRules = {
mongoObjectId : function(name, value, schema) {
// Applies a regex for mongo object ids
var pattern = "^[a-zA-Z0-9]{24}$";
if(name.toUpperCase().indexOf("ID") != -1 && typeof value == "string" && new RegExp(pattern).test(value)) {
schema.pattern = pattern;
}
return schema;
},
uuid : function(name, value, schema) {
// Applies a regex for mongo object ids
var pattern = "^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$";
if(name.toUpperCase().indexOf("ID") != -1 && typeof value == "string" && new RegExp(pattern).test(value)) {
schema.pattern = pattern;
}
return schema;
},
currency : function(name, value, schema) {
// Applies a regex for mongo object ids
var pattern = "^[A-Z]{3}$";
if(name.toUpperCase().indexOf("CURRENCY") != -1 && typeof value == "string" && new RegExp(pattern).test(value)) {
schema.pattern = pattern;
}
return schema;
}
}
var rulesToApply = []; // TODO: Filter
for(prop in availableRules) {
if(typeof availableRules[prop] === "function") {
rulesToApply.push(availableRules[prop]);
}
}
console.log("Starting processing...");
function processFieldByType(fieldName, fieldValue) {
switch(typeof fieldValue){
case "string":
return processString(fieldValue);
case "number":
return processNumber(fieldValue);
case "boolean":
return processBoolean(fieldValue);
case "object":
if(Array.isArray(fieldValue)) {
return processArray(fieldValue);
} else {
return processObject(fieldValue);
}
default:
console.log("Unknown type. Field: " + fieldName + ". Type: " + typeof fieldValue);
}
return null;
}
function applyRules(fieldName, fieldValue, fieldSchema) {
for(var i=0;i<rulesToApply.length;i++) {
fieldSchema = rulesToApply[i](fieldName, fieldValue, fieldSchema);
}
return fieldSchema;
}
function processField(fieldName, fieldValue) {
return applyRules(fieldName, fieldValue, processFieldByType(fieldName, fieldValue))
}
function processString(str) {
// TODO: RegEx pattern
return {"type": "string"};
}
function processBoolean(bool) {
return {"type": "boolean"};
}
function processArray(arr) {
var sch = {"type": "array"};
// TODO: Support multiple types using anyOf
if(arr.length > 0) {
sch.items = {}
sch.items = processField("ArrayItem", arr[0]);
}
return sch;
}
function processNumber(num) {
var sch = {};
//TODO: RegEx?
if(num.toString().indexOf(".") == -1) {
sch.type = "integer";
}
else {
sch.type = "number";
}
return sch;
}
function processObject(obj) {
var objSch = {"type": "object", "properties": {}};
var propName; // To prevent recursive methods changing it
for(prop in obj) {
propName = prop;
objSch.properties[prop] = processField(prop, obj[prop]);
}
return objSch;
}
function process() {
// Try to parse the input json
try {
var src = JSON.parse(document.getElementById('src').value);
document.getElementById('src').value = JSON.stringify(src, null, 2);
}
catch (err) {
alert("Error while parsing input:" + err);
}
// Create the schema root object
var schema = {
"$schema": "#",
"id": "http://YOUR_ROOT_URL/schemas/SCHEMA_NAME.json",
"type": "object"
}
var rootObj= processObject(src);
schema["properties"] = rootObj["properties"];
// Output the schema
var tgt = JSON.stringify(schema, null, 2);
console.log(tgt);
document.getElementById('tgt').value = tgt;
console.log("Processing complete.");
}