Hi, all,
i'm tinkering with some generic JS-side argument validation code and thought it might interest a couple of you. i'm not yet sure what it's for, and there's probably room for improvement, but it seems to work as it is now...
function applyValidation( func, rules ){
return function(){
var av = Array.prototype.slice.apply(arguments,[0]);
rules( av );
return func.apply( this, av );
}
};
JSON.stringify = applyValidation( JSON.stringify,
function(a){
if(!a.length) throw new Error("!arguments.length");
});
All that this does is shove argument validation rules between some function and arbitrary clients.
Now this throws: JSON.stringify()
but this doesn't: JSON.stringify({})
i'm not yet sure if it's useful, but it seems kinda interesting. My original attempt was to add a .validation property to arguments.callee, and reimplement Function.prototype.apply to check for these rules, but it seems i can't simply swap out Function.prototype.apply.
Happy Hacking!
--
----- stephan beal
http://wanderinghorse.net/home/stephan/