开发者

jQuery/Javascript: How can I evaluate the validity of function arguments efficiently with a range of valid types?

I've got a rather large plugin that I am currently writing in jQuery which is using a lot of internal functions that can accept varying arguments depending on the function.

I caught myself constantly writing the following in every function to stop the code from running if an argument hasn't been supplied or isn't valid:

add : function(args) { 
    if (args===undefined) return;
    // function code;
},...

I was hoping that in a DRY type of sense it would be a good idea to write a little internal helper function that would do this for me.

Is this actually a good idea and most importantly what is the best/secure way t开发者_开发百科o check for a varied range of acceptable arguments?

There are a lot of functions with multiple arguments in this plugin, for example:

load : function( filename , path , excludeFromRandom , callback ) {}

where filename is a string, path is a string, excludeFromRandom is a boolean and callback can be a function or a string.

What is a good way to check for the existence and validity of these types of arguments without rewriting the same code over and over?

Any suggestions and ideas would be great.

Thanks for reading.


It depends to what extent you want to do this. In idea would be to create a validation function which takes a argument -> rule mapping. E.g.:

function foo(somestring, somenumber) {

    var rules = {
       'somestring': Validator.rules.isString,
       'somenumber': Validator.rules.inRange(5,10);
    };


} 

Validator would contain the basic logic and some helper functions (rules):

var Validator = {
    valid: function(args, rules) {
        for(var name in rules) {
            if(!rules[name](args[name])) {
                return false;
            }
        }
        return true;
    },
    rules: {
        isString: function(arg) {
            return (typeof arg === 'string');
        },
        inRange: function(x,y) {
            return function(arg) {
                return !isNaN(+arg) && x <= arg && arg <= y;
            }
        }
    }
}

This is just a sketch, it certainly can be extended (like accepting multiple rules per argument), but it should give you some idea.

That said, you don't have to check every argument. Provide decent documentation. If people use your plugin in a wrong way, i.e. passing wrong argument types, then your code will throw an error anyway.

Update:

If want to do this very often, then a good idea is to write a wrapper function and you just pass the function and the rules to it:

function ensure(func, rules, context) {
    context = context || this;
    return function() {
        if(Validator.valid(arguments, rules)) {
            return func.apply(context, arguments);
        }
        return null; // or throw error, whatever you want
    }
}

Then you can define your function normally as:

var foo = function(somestring, somenumber) {
    // ...
};

and just add validation to it:

var rules = {...};
foo = ensure(foo, rules);

You could even consider to make ensure accept a callback which gets called on error or success of the function, instead of returning a value. There are a lot of possibilities.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜