Tech Blog

Jay's Technical blog

MSAjax ClientScript - unpublished validation function

26 April 2007
Jay Kimble

[WARNING! This is an archived post and as such there may be things broken/missing here.. you have been warned.]

[I just did a quick search on the net to see if anyone has published anything on this (and no one has), so here we go.]

One of the problems with Javascript that is commonly complained about is type safety; I know having dynamic types is also a feature, but what happens when you want to make sure that something is of a certain type. Well, you can always try to convert the types to the necessary type or at least check the value of the type... using the getType method (available on the every object using the MS Ajax Javascript) also can help.

But wouldn't it be nice if there was simply a way to check the types in a very convenient way to validate the type (and throw an exception if something is off). Believe it or not (I'm sure you do) the MS Ajax Extensions have an unpublished method for doing this: Function._validateParams. if takes 2 arguments : arguments array, and an object describing the parameters you want to validate. Here's a simple example to get us started:

function myFunction(aName, somethingElse) {
    var e = Function._validateParams(arguments, [
         {name: "aName", type: String}, 
         {name: "somethingElse", mayBeNull: true} ]); 
    if (e) throw e;
}


So let's dig into that second parameter... it's a JSON string. If you don't know what that looks like then now you know... I really don't want to get into a huge primer on JSON, but here's a couple siimple rules so you can read that... anything between "[" and "]" denotes an array; anything between "{" and "}" is an object with properties identified by "name:value" (there's more to it, but you can now read the JSON above...

The second parameter is an array of objects.. one object for each parameter. So let's tear apart the individual expected parameter object within the array. All of the parameter objects have a name field (this is so the validation script can identify the element by name from the arguments array). In the example above we have an additional field. The "aName" param has a type of String (BTW, this can be any type such as Function, Number, Sys.Component, etc.), so this parameter's type will be checked by the validation routine to ensure that only strings are passed in. The "somethingElse" field has a mayBeNull field (boolean) which indicates that null values are allowed in this field.

Here's the complete list of additional fields:

type- (Type) indicates the allowed type for this parameter (Some examples are Array, Function, String, Sys.Component, etc.)
mayBeNull -(boolean) indicates that a param may have null passed in

integer - (boolean) indicates that a param whose accepted type is number is also specifically tested to make sure it is an integer
domElement
- (boolean) indicates that the object param is a domElement
optional - (boolean) indicates that the parameter is optional
elementType - (Type) - indicates the accepted type of all elements of an Array passed in (type must be Array in this case)
elementInteger
- (boolean) indicates that all array elements are also specifically tested to make sure it is an integer
elementDomElement
- (boolean) indicates that all array elements are a domElement
elementMayBeNull -(boolean) indicates that an array element may have null passed in.

Something that should be mentioned is that one of the things that happens is that the routine will make sure that the number of parameters passed to the function are within the correct range (parameters can be marked as being optional, so it checks to make sure that the number of parameters passed in is within the max and min number of parameters expected); beware if this fact; if you want to validate you need to validate all the parameters.

BTW, the routine returns an exception object to you if there was a problem with the parameters passed in. If you want to handle the exception in your function you can or you can simply throw it back to the caller.

Just so you can see how complex you can make things... let's look at one final more complicated example:

function myFunction(aNumber, somethingElse) {
    var e = Function._validateParams(arguments, [
        {name: "aNumber", type: Array, elementType: Number, elementInteger: true, elementMayBeNull: false}, 
        {name: "somethingElse", type: Object, domElement:true, optional: true, mayBeNull: true} ]); 
    if (e) throw e; }

This last example was to show you that you can throw all kinds of options into the JSON. We still have 2 parameters that will be passed in aNumber and somethingElse. aNumber is an array of integers (Numbers), and all elements must have a value (BTW, the elementMustBeNull is really not required in this case; generally you would only use it if you wanted to allow nulls). somethingElse is domElement, but it doesn't have to be passed in or it can be null.

kick it on DotNetKicks.com

[tags:MS Ajax Extensions, AJax, ASP.NET, JavaScript, ClientScript]


Pondering Large Corporations and programming...

09 April 2007
Jay Kimble

[WARNING! This is an archived post and as such there may be things broken/missing here.. you have been warned.]

[Sorry if my original posting of this was a little unreadable... My blog writer evidently didn't make the transit to the new OS all that well (see below for what I'm talking about)] 

I've had an interesting couple of months as a developer and a "Microsoft guy" (possibly "MS fanboy"). What pray tell could have done this... well, I have a friendship with Mike Gunderloy who back in January announced that he was moving away from the MS stack (entirely). At the time I was working with Mike on some writing and was somewhat privy to his feelings.

Here's the summary. Mike feels that MS has recently made some power grabs that are actually dangerous for the industry on the whole (both with the MS Office 2007 Ribbon and in the Vista EULA). Whether he knows it or not Mike is an influence in my programming life (he's helped in a number ways during my whole transition to an Indy programmer... BTW, I'm always interested in new work).

Anyway, I've really been considering where Mike is at. He's right there have been some things that are alarming. Do I think Joe average programmer (or Product Manager) is working to do this? Heck no! I think ScottGu is the man and he's taken development in my opinion to new heights, but MS legal is a different thing altogether.

So I find myself at a juncture where I'm pursuing a number of MS technologies (and possibly an MVP, but who knows it that will ever happen), and yet I'm concerned about some of this stuff.

My particulars are that I really think the Office 2007 Ribbon UI License sounds fishy to me and the DRM elements that were announced in the EULA were also a little concerning (at least the lawyers description of them).

Right now I sound a lot like any number of SlashDotters.

On Friday, I went to the DotNet University in Tampa where I hooked up with my blog father Joe Healy. He's deeply concerned about something else that is touching my life... MS is no problem to him, but that's because he's a 'softie... He's the Dev Evangelist for this area (South East of the US, I believe).

The Other Evil Empire
Anyway he got me thinking about that other large corporation... the one that almost everyone who criticizes MS is OK with. You've probably heard some conspiracy theories about them as well. In case you haven't guess the other corporation is Google. It's almost an unconscious thought to type google before searching... turning the name of the company into a verb... or better yet, how about letting them house your data (and then market to you)... sure you know that's happening...

I know you're probably thinking "come on Jay.. Google???" They give everything away. They're nice. They'd never do anything shady...

Oh really? How about the whole book fiasco where Google said "We're putting books on line and we don't care what you think."

My point is that Google is not above doing something shady. So does that mean that I think you should avoid Google and MS or just MS or just Google?

I think you should look at the situation and realize that there are marketers and lawyers behind this company's efforts (all company's efforts... including mine). You should then minimize your own heavy reliance on any of them (or at least realize that you are consciously jumping into bed with that company).

So... I installed Vista on my laptop this weekend and I've been running Office 2007 for about a month. I watch MS, and am careful of what I'm supporting... I'm vocal about what I think is not right. I'll probably minimize my support of them in some ways (although I'm less worried about them since I read the EULA and didn't see any mention of DRM).

I'm also going to continue to use Gmail, but will work at trying to cut the ties on some of the other google products I use including search. In fact I decided to not switch my IE7 over to using Google Search by default.

I have to have both companies in my life right now (and I believe I will for the foreseeable future), but I'm going to do my best to minimize their total impact.

I am in fact still an "MS fanboy" and will continue to be because I know what MS is doing; MS also has so many Gov't agencies watching them so closely it's not even funny... Google on the other hand has no one watching what they are doing (and no one really knows).

[tags: Microsoft, Google, Evil Empire]


A Day of Ajax... coming soon to Tampa...

06 April 2007
Jay Kimble

[WARNING! This is an archived post and as such there may be things broken/missing here.. you have been warned.]

[I'm starting the buzz as early as I can...]

I am in the process of putting together a free fullday training in Tampa called "A Day of Ajax" with Joe Healy (my Blog Daddy), Jim Zimmerman, and Jon Goodyear (and ASPSOFT). The presenter list has not been finalized yet (please, Joe, please get that special guest...)

I can tell you that I am planning a killer best practices session.

Anyway, we are planning for mid-to-late June, so watch this blog for further info.

[tags:ASP.NET,MS Ajax]