Tags

MicrosoftAjax.js is still my favourite framework. One of the reason is the Function.validateParameters, which allows me to validate parameters type in JavaScript. However, as it seems more and more developers are choosing other frameworks, I have decided to create my own validation, off course without pretend is so reliable like the one Microsoft did.

The code includes a namespace called “System” with two classes: “Object” and “Function”. “Object” class includes only one method to get type name for provided parameter.

var System = System || {};


System.Object = {}

System.Object.getTypeName = function(instance) {   
    

    
    var ctor = instance.constructor;
    
    // Instance is string
    if(ctor.toString().indexOf('function String()') !== -1) {
        return 'String';
    }
    
    // Instance is array
    if(ctor.toString().indexOf('function Array()') !== -1) {
        return 'Array';
    }
    
    // Instance is object
    if(ctor.toString().indexOf('function Object()') !== -1) {
        return 'Object';
    }
    
    // Instance is function
     if(ctor.toString().indexOf('function Function()') !== -1) {
        return 'Function';
    } 
    
     // Instance is number
     if(ctor.toString().indexOf('function Number()') !== -1) {
        return 'Number';
    }
    
     // Instance is date
     if(ctor.toString().indexOf('function Date()') !== -1) {
        return 'Date';
    }   
    
    return 'Variant';

}

“Function” class contains all other methods to validate parameters passed to a function.

System.Function = {};

System.Function.validateParameters = function (parameters, expectedParameters) {
    

    
     for(var i = 0; i < expectedParameters.length; i++) {
         
         var mayBeNull = expectedParameters[i]['mayBeNull'] || false;
         var optional = expectedParameters[i]['optional'] || false;
         var type =  expectedParameters[i]['type'] || 'Variant';
         var name = expectedParameters[i]['name'];
         var e = System.Function.validateParameterType(parameters[i],type,mayBeNull,optional);
         if(e === false) {
             return false;
         }
         
     }     
     
     return true;
     
}




System.Function.validateParameterType = function(param, type, mayBeNull, optional) {
    
    // Param is null
    if(param === null) {
       return mayBeNull === false ? false : true;
    } 
   
   // Param is undefined
    if(typeof(param) === 'undefined') {
       return optional === false ? false : true;
       
    }

   
   if(type !== 'Variant') {
        // Validate parameter type
        var paramType = System.Object.getTypeName(param);
        if(paramType !== type) {
            return false;
        }
    }
   return true;
}

Usage is similar with MicrosftAjax.js, the only difference is I provided type of the object in a string, not global object itself.

function testValidation(param1, param2, param3, param4) {
    var e = System.Function.validateParameters(arguments,
            [{type:'String', name: 'param1', optional: false },
             {type:'Number', name: 'param2', optional: false },
             {type:'Object', name: 'param3', optional: false },
             {type:'Array', name: 'param4', optional: false }]);

    if(e === true) throw 'Error message here';
}


testValidation('asdasd', 1, {}, []);

However, this might be a little bit primitive, so taking a look into Microsoft Library, validation can be done in a more elegant way.

Object._get_type = function(instance) {
    var ctor = instance.constructor;
    if (!ctor || (typeof(ctor) !== "function")) {
        return Object;
    }
    return ctor;
};

Object.get_type = function(instance) {
    return Object._get_type(instance).name;
};

And this seems to work as expected.

// Alert String
var oString = 'text';
alert(Object.get_type(oString));

// Alert Number
var oNumber = 1;
alert(Object.get_type(oNumber));

// Alert Function
var oFunction = function() {};
alert(Object.get_type(oFunction));

// Alert Date
var oDate = new Date();
alert(Object.get_type(oDate));

// Alert Array
var oArray = [];
alert(Object.get_type(oArray));

// Alert Object
var oObject = {};
alert(Object.get_type(oObject));
Advertisements