Project

General

Profile

Statistics
| Revision:

root / trunk / web / dojo / dojox / json / schema.js @ 10

History | View | Annotate | Download (4.52 KB)

1 9 andrej.cim
/*
2
        Copyright (c) 2004-2010, The Dojo Foundation All Rights Reserved.
3
        Available via Academic Free License >= 2.1 OR the modified BSD license.
4
        see: http://dojotoolkit.org/license for details
5
*/
6
7
8
if(!dojo._hasResource["dojox.json.schema"]){
9
dojo._hasResource["dojox.json.schema"]=true;
10
dojo.provide("dojox.json.schema");
11
dojox.json.schema.validate=function(_1,_2){
12
return this._validate(_1,_2,false);
13
};
14
dojox.json.schema.checkPropertyChange=function(_3,_4,_5){
15
return this._validate(_3,_4,_5||"property");
16
};
17
dojox.json.schema.mustBeValid=function(_6){
18
if(!_6.valid){
19
throw new TypeError(dojo.map(_6.errors,function(_7){
20
return "for property "+_7.property+": "+_7.message;
21
}).join(", "));
22
}
23
};
24
dojox.json.schema._validate=function(_8,_9,_a){
25
var _b=[];
26
function _c(_d,_e,_f,i){
27
var l;
28
_f+=_f?typeof i=="number"?"["+i+"]":typeof i=="undefined"?"":"."+i:i;
29
function _10(_11){
30
_b.push({property:_f,message:_11});
31
};
32
if((typeof _e!="object"||_e instanceof Array)&&(_f||typeof _e!="function")){
33
if(typeof _e=="function"){
34
if(!(Object(_d) instanceof _e)){
35
_10("is not an instance of the class/constructor "+_e.name);
36
}
37
}else{
38
if(_e){
39
_10("Invalid schema/property definition "+_e);
40
}
41
}
42
return null;
43
}
44
if(_a&&_e.readonly){
45
_10("is a readonly field, it can not be changed");
46
}
47
if(_e["extends"]){
48
_c(_d,_e["extends"],_f,i);
49
}
50
function _12(_13,_14){
51
if(_13){
52
if(typeof _13=="string"&&_13!="any"&&(_13=="null"?_14!==null:typeof _14!=_13)&&!(_14 instanceof Array&&_13=="array")&&!(_13=="integer"&&_14%1===0)){
53
return [{property:_f,message:(typeof _14)+" value found, but a "+_13+" is required"}];
54
}
55
if(_13 instanceof Array){
56
var _15=[];
57
for(var j=0;j<_13.length;j++){
58
if(!(_15=_12(_13[j],_14)).length){
59
break;
60
}
61
}
62
if(_15.length){
63
return _15;
64
}
65
}else{
66
if(typeof _13=="object"){
67
var _16=_b;
68
_b=[];
69
_c(_14,_13,_f);
70
var _17=_b;
71
_b=_16;
72
return _17;
73
}
74
}
75
}
76
return [];
77
};
78
if(_d===undefined){
79
if(!_e.optional){
80
_10("is missing and it is not optional");
81
}
82
}else{
83
_b=_b.concat(_12(_e.type,_d));
84
if(_e.disallow&&!_12(_e.disallow,_d).length){
85
_10(" disallowed value was matched");
86
}
87
if(_d!==null){
88
if(_d instanceof Array){
89
if(_e.items){
90
if(_e.items instanceof Array){
91
for(i=0,l=_d.length;i<l;i++){
92
_b.concat(_c(_d[i],_e.items[i],_f,i));
93
}
94
}else{
95
for(i=0,l=_d.length;i<l;i++){
96
_b.concat(_c(_d[i],_e.items,_f,i));
97
}
98
}
99
}
100
if(_e.minItems&&_d.length<_e.minItems){
101
_10("There must be a minimum of "+_e.minItems+" in the array");
102
}
103
if(_e.maxItems&&_d.length>_e.maxItems){
104
_10("There must be a maximum of "+_e.maxItems+" in the array");
105
}
106
}else{
107
if(_e.properties){
108
_b.concat(_18(_d,_e.properties,_f,_e.additionalProperties));
109
}
110
}
111
if(_e.pattern&&typeof _d=="string"&&!_d.match(_e.pattern)){
112
_10("does not match the regex pattern "+_e.pattern);
113
}
114
if(_e.maxLength&&typeof _d=="string"&&_d.length>_e.maxLength){
115
_10("may only be "+_e.maxLength+" characters long");
116
}
117
if(_e.minLength&&typeof _d=="string"&&_d.length<_e.minLength){
118
_10("must be at least "+_e.minLength+" characters long");
119
}
120
if(typeof _e.minimum!==undefined&&typeof _d==typeof _e.minimum&&_e.minimum>_d){
121
_10("must have a minimum value of "+_e.minimum);
122
}
123
if(typeof _e.maximum!==undefined&&typeof _d==typeof _e.maximum&&_e.maximum<_d){
124
_10("must have a maximum value of "+_e.maximum);
125
}
126
if(_e["enum"]){
127
var _19=_e["enum"];
128
l=_19.length;
129
var _1a;
130
for(var j=0;j<l;j++){
131
if(_19[j]===_d){
132
_1a=1;
133
break;
134
}
135
}
136
if(!_1a){
137
_10("does not have a value in the enumeration "+_19.join(", "));
138
}
139
}
140
if(typeof _e.maxDecimal=="number"&&(_d.toString().match(new RegExp("\\.[0-9]{"+(_e.maxDecimal+1)+",}")))){
141
_10("may only have "+_e.maxDecimal+" digits of decimal places");
142
}
143
}
144
}
145
return null;
146
};
147
function _18(_1b,_1c,_1d,_1e){
148
if(typeof _1c=="object"){
149
if(typeof _1b!="object"||_1b instanceof Array){
150
_b.push({property:_1d,message:"an object is required"});
151
}
152
for(var i in _1c){
153
if(_1c.hasOwnProperty(i)&&!(i.charAt(0)=="_"&&i.charAt(1)=="_")){
154
var _1f=_1b[i];
155
var _20=_1c[i];
156
_c(_1f,_20,_1d,i);
157
}
158
}
159
}
160
for(i in _1b){
161
if(_1b.hasOwnProperty(i)&&!(i.charAt(0)=="_"&&i.charAt(1)=="_")&&_1c&&!_1c[i]&&_1e===false){
162
_b.push({property:_1d,message:(typeof _1f)+"The property "+i+" is not defined in the schema and the schema does not allow additional properties"});
163
}
164
var _21=_1c&&_1c[i]&&_1c[i].requires;
165
if(_21&&!(_21 in _1b)){
166
_b.push({property:_1d,message:"the presence of the property "+i+" requires that "+_21+" also be present"});
167
}
168
_1f=_1b[i];
169
if(_1c&&typeof _1c=="object"&&!(i in _1c)){
170
_c(_1f,_1e,_1d,i);
171
}
172
if(!_a&&_1f&&_1f.$schema){
173
_b=_b.concat(_c(_1f,_1f.$schema,_1d,i));
174
}
175
}
176
return _b;
177
};
178
if(_9){
179
_c(_8,_9,"",_a||"");
180
}
181
if(!_a&&_8&&_8.$schema){
182
_c(_8,_8.$schema,"","");
183
}
184
return {valid:!_b.length,errors:_b};
185
};
186
}