jQuery array of functions validation I am attempting to validate a set of business rules in order for a user to successfully add to cart. The rules vary from requiting to add/decline a requested add-on to not having more then 60 items in your shopping cart.

For my end goal, I need to be able to have several dozen rule sets and if any return false then execute the needed function and prevent cart submit.

Separation of the rules logic seemed like a great thing so that I do not clutter my plugin or ajax call it's self, but I wonder if there is a better way because I tend to over engineer things.

JS Fiddle example

Here is the basic object:

var req = {
    type: function(name){
        switch(typeof name){
            case "function":
                return "function";
            break;
            case "object":
                if($.isArray(name)){
                    return "array";
                }else{
                    return "object";
                };
            break;
            case "string":
                return "string";
            break;
            case "number":
                return "number";
            break;
            case '':
            case "undefined":
            default:
                return "undefined";
            break;
        };
    },
    check: function(v){
        var valid = new Array();

        if(this.type(v) === "array"){
            if(v.length &gt; 0){
                for(var i = 0; i &lt; v.length; i++){
                    if(this.type(v[i]) === "function"){
                        valid.push(v[i]());
                    };
                };
            }else{
                return true;
            };
        }else{
            if(this.type(v) === "function"){
                valid.push(v());
            };
        };

        if(valid.indexOf(false) &gt; -1){
            return false;
        }else{
            return true;
        };
    }
};


Here is how the basic functions are pushed into the array:

var addHelper = {
    invoke: function(type, fun){
        gb[type].push(fun);
    }
};

addHelper.invoke("validate",
    function(){
        if(parseInt($("#fakeQty").val()) &gt; 59){
            var qtyMsg = "For bulk or commercial orders, please call our online support";
            alert(qtyMsg);
            return false;
        }else{
            return true;
        };
    }
);


Each function is stored in its own object, but for space sake I just simple passed it directly.
The type function is definitely overengineered in this case because you only need two values. The entire first snippet boils down to a simple function and the logic is instantly observable.

var req = {
    check: function(v){
        if (typeof v === "function") {
            return e();
        }
        if (typeof v === "array" || $.isArray(v)) {
            return v.every(function(e) {
                return typeof e !== "function" || e();
            });
        }
        return true;
    }
}


Note, there's no need for semicolons after closing curly braces of if, for, so if anything it shows you don't understand the automatic semicolon insertion rules.