日期:2014-05-16  浏览次数:20417 次

验证JavaScript函数的参数

一段简单代码,用于验证JavaScript函数中的参数个数,类型等

?

使用方法:

在每个函数执行前调用验证函数,如果验证未通过,验证函数将返回true,否则返回false或者null

?

?

String.__typeName = "string";
Number.__typeName = "number";
Array.__typeName = "array";
Function.__typeName = "function";
Object.__typeName = "object";

ffap = {};

ffap.Utils = {
    _isInstanceOfType: function(type, instance){
        if (type === String) {
            return (typeof instance === "string");
        }
        if (type === Number) {
            return (typeof instance === "number");
        }
        if (type === Boolean) {
            return (typeof instance === "boolean");
        }
        return instance instanceof type;
    },
    
    /**
     * 验证函数参数
     *
     * @param {Object} params			待验证的参数
     * @param {Object} expectedParams	期望的参数
     * @param {Object} validateParameterCount	是否验证参数个数,默认为true
     */
    validateParams: function(params, expectedParams, validateParameterCount){
        /* 待验证的参数个数 */
        var expectedLength = expectedParams.length, error = false;
        validateParameterCount = validateParameterCount !== false;
        
        error = ffap.Utils._validateParameterCount(params, expectedParams, validateParameterCount);
        if (error) {
            return true;
        }
        
        for (var i = 0, l = params.length; i < l; i++) {
            var expectedParam = expectedParams[Math.min(i, expectedLength - 1)], paramName = expectedParam.name;
            if (expectedParam.parameterArray) {
                paramName += "[" + (i - expectedLength + 1) + "]";
            }
            else 
                if (!validateParameterCount && (i >= expectedLength)) {
                    break;
                }
            error = ffap.Utils._validateParameter(params[i], expectedParam, paramName);
            if (error) {
                return error;
            }
        }
        return false;
    },
    
    
    _validateParameterCount: function(params, expectedParams, validateParameterCount){
        var i, error, expectedLen = expectedParams.length, actualLen = params.length;
        if (actualLen < expectedLen) {
            var minParams = expectedLen;
            for (i = 0; i < expectedLen; i++) {
                var param = expectedParams[i];
                if (param.optional || param.parameterArray) {
                    minParams--;
                }
            }
            if (actualLen < minParams) {
                error = true;
            }
        }
        else 
            if (validateParameterCount && (actualLen > expectedLen)) {
                error = true;
                for (i = 0; i < expectedLen; i++) {
                    if (expectedParams[i].parameterArray) {
                        error = false;
                        break;
                    }
                }
            }
        if (error) {
            console.log("参数个数不正确");
            return true;
        }
        
        return false;
    },
    
    _validateParameter: function(param, expectedParam, paramName){
        var error, expectedType = expectedParam.type, expectedInteger = !!expectedParam.integer, expectedDomElement = !!expectedParam.domElement, mayBeNull = !!expectedParam.mayBeNull;
        
        error = ffap.Utils._validateParameterType(param, expectedType, expectedInteger, expectedDomElement, mayBeNull, paramName);
        if (error) {
            return error;
        }
        
        var expectedElementType = expectedParam.elementType, elementMayBeNull = !!expectedParam.elementMayBeNull;
        
        if (expectedType === Array && typeof(param) !== "undefined" && param !== null &&
        (expectedElementType || !elementMayBeNull)) {
            var expectedElementInteger = !!expectedParam.elementInteger, expectedElementDomElement = !!expectedParam.elementDomElemen