| 1 |
(function(){ |
| 2 |
var jsonSchema, inDojo = typeof dojo != "undefined"; |
| 3 |
if(inDojo){ |
| 4 |
dojo.provide("dojox.lang.typed"); |
| 5 |
dojo.require("dojox.json.schema"); |
| 6 |
jsonSchema = dojox.json.schema; |
| 7 |
}else{ |
| 8 |
if(typeof JSONSchema == "undefined"){ |
| 9 |
throw new Error("Dojo or JSON Schema library must be present"); |
| 10 |
} |
| 11 |
jsonSchema = JSONSchema; |
| 12 |
} |
| 13 |
function validatingFunction(func, getMethodDef){ |
| 14 |
var validatingFunc = function(){ |
| 15 |
var methodDef = getMethodDef(); |
| 16 |
if(methodDef && methodDef.parameters){ |
| 17 |
var params = methodDef.parameters; |
| 18 |
for(var j = 0; j < params.length; j++){ |
| 19 |
arguments[j] = validate(arguments[j], params[j], j.toString()); |
| 20 |
} |
| 21 |
if(methodDef.additionalParameters){ |
| 22 |
for(;j < arguments.length; j++){ |
| 23 |
arguments[j] = validate(arguments[j], methodDef.additionalParameters, j.toString()); |
| 24 |
} |
| 25 |
} |
| 26 |
} |
| 27 |
var returns = func.apply(this, arguments); |
| 28 |
if(methodDef.returns){ |
| 29 |
validate(returns, methodDef.returns); |
| 30 |
} |
| 31 |
return returns; |
| 32 |
}; |
| 33 |
validatingFunc.__typedFunction__ = true; |
| 34 |
for(var i in func){ |
| 35 |
validatingFunc[i] = func[i]; |
| 36 |
} |
| 37 |
return validatingFunc; |
| 38 |
} |
| 39 |
function identityFunc(obj){ |
| 40 |
return function(){ |
| 41 |
return obj; |
| 42 |
} |
| 43 |
} |
| 44 |
function validate(instance, schema, property){ |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
if(typeof instance == "function" && schema && !instance.__typedFunction__){ |
| 50 |
instance = validatingFunction(instance, identityFunc(schema)); |
| 51 |
} |
| 52 |
var result = jsonSchema._validate(instance, schema, property); |
| 53 |
if(!result.valid){ |
| 54 |
var errorMessage = "" |
| 55 |
var errors = result.errors; |
| 56 |
for(var i = 0; i < errors.length; i++){ |
| 57 |
errorMessage += errors[i].property + ' ' + errors[i].message + '\n'; |
| 58 |
} |
| 59 |
throw new TypeError(errorMessage); |
| 60 |
} |
| 61 |
return instance; |
| 62 |
} |
| 63 |
var hasGetters = jsonSchema.__defineGetter__; |
| 64 |
var typedFunction = function(Class){ |
| 65 |
|
| 66 |
|
| 67 |
if(Class.__typedClass__){ |
| 68 |
|
| 69 |
return Class; |
| 70 |
} |
| 71 |
var Wrapper = function(){ |
| 72 |
var i, value, properties = Wrapper.properties; |
| 73 |
var methods = Wrapper.methods; |
| 74 |
Class.apply(this,arguments); |
| 75 |
this.__props__ = {}; |
| 76 |
for(i in methods){ |
| 77 |
value = this[i]; |
| 78 |
if(value){ |
| 79 |
if(!value.__typedFunction__){ |
| 80 |
|
| 81 |
var proto = this; |
| 82 |
while(!proto.hasOwnProperty(i) && proto.__proto__){ |
| 83 |
proto = proto.__proto__; |
| 84 |
} |
| 85 |
(function(i){ |
| 86 |
proto[i] = validatingFunction(value, function(){ |
| 87 |
return methods[i]; |
| 88 |
}); |
| 89 |
})(i); |
| 90 |
} |
| 91 |
}else{ |
| 92 |
(function(i){ |
| 93 |
this[i] = function(){ |
| 94 |
throw new TypeError("The method " + i + " is defined but not implemented"); |
| 95 |
}; |
| 96 |
})(i); |
| 97 |
} |
| 98 |
} |
| 99 |
if(hasGetters){ |
| 100 |
var self = this; |
| 101 |
for(i in properties){ |
| 102 |
|
| 103 |
value = this[i]; |
| 104 |
if(this.hasOwnProperty(i)){ |
| 105 |
this.__props__[i] = value; |
| 106 |
} |
| 107 |
(function(i){ |
| 108 |
delete self[i]; |
| 109 |
self.__defineGetter__(i, function(){ |
| 110 |
return i in this.__props__ ? this.__props__[i] : this.__proto__[i]; |
| 111 |
}); |
| 112 |
self.__defineSetter__(i, function(value){ |
| 113 |
validate(value, properties[i], i); |
| 114 |
return this.__props__[i] = value; |
| 115 |
}); |
| 116 |
})(i); |
| 117 |
} |
| 118 |
} |
| 119 |
validate(this, Wrapper); |
| 120 |
}; |
| 121 |
Wrapper.prototype = Class.prototype; |
| 122 |
for(var i in Class){ |
| 123 |
Wrapper[i] = Class[i]; |
| 124 |
} |
| 125 |
if(Class.prototype.declaredClass && inDojo){ |
| 126 |
dojo.setObject(Class.prototype.declaredClass, Wrapper); |
| 127 |
} |
| 128 |
Wrapper.__typedClass__ = true; |
| 129 |
return Wrapper; |
| 130 |
}; |
| 131 |
if(inDojo){ |
| 132 |
dojox.lang.typed = typedFunction; |
| 133 |
if(dojo.config.typeCheckAllClasses){ |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
var defaultDeclare = dojo.declare; |
| 139 |
dojo.declare = function(name){ |
| 140 |
var clazz = defaultDeclare.apply(this, arguments); |
| 141 |
clazz = typedFunction(clazz); |
| 142 |
return clazz; |
| 143 |
}; |
| 144 |
dojo.mixin(dojo.declare, defaultDeclare); |
| 145 |
} |
| 146 |
}else{ |
| 147 |
typed = typedFunction; |
| 148 |
} |
| 149 |
})(); |