forked from stemey/dojo-generate-form
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconvertSchema.js
81 lines (76 loc) · 2.13 KB
/
convertSchema.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
define([ "dojo/_base/array", //
"dojo/_base/lang",//
"dojo/_base/declare",//
"dojox/json/ref",
], function(array, lang, declare, ref) {
var formatToTypeMapping= {
date:"date",
time:"time",
"date-time":"date-time"
}
var convert = {
copy: function(propSource,propTarget,prop,attribute) {
var value = prop[propSource];
if (value) {
attribute[propTarget]=value;
}
},
convertAttribute: function(prop,attribute) {
//attribute.type=prop.type;
this.copy("description","description",prop,attribute);
this.copy("title","label",prop,attribute);
this.copy("required","required",prop,attribute);
this.copy("readonly","readonly",prop,attribute);
this.copy("enum","values",prop,attribute);
if (prop.type=="object") {
var types = this.convertObjectProp(prop);
attribute.type="object";
attribute.validTypes=types;
}else if (prop.type=="array") {
attribute.array=true;
if (prop.items.type=="object") {
attribute.type="object";
var types = this.convertObjectProp(prop.items);
attribute.validTypes=types;
} else if (prop.items.type=="array") {
throw new Error("cannot convert array of arrays");
}else{
attribute.type=prop.items.type;
}
}else{
var type = formatToTypeMapping[prop.format];
attribute.type=type || prop.type;
}
},
convertObjectProp: function(prop) {
if (prop.oneOf) {
return this.convertTypes(prop.oneOf);
}else if (prop.properties) {
return [this.convert(prop)];
}else{
throw new Error("cannot only convert objects with oneOf");
}
},
convertTypes: function(schemas) {
return array.map(schemas,lang.hitch(this,"convert"),this);
},
convert: function(schema,meta) {
schema=ref.resolveJson(schema);
meta={};
meta.attributes=[];
//return meta;
for (var key in schema.properties) {
// "__parent" is added by dojox/json/ref
if (key!="__parent") {
var attribute={};
attribute.code=key;
var prop = schema.properties[key];
meta.attributes.push(attribute);
this.convertAttribute(prop,attribute);
}
}
return meta;
}
}
return lang.hitch(convert,"convert");
});