Home

Welcome

This javascript library helps validating function arguments.
It has some predefined types and can be easy extended to support more.

There is no any license. So feel free to download, use and modified it.
The package includes qunit tests. There you can see examples how to use it.

How to use it

Pass arguments or Array to Arg.testArguments as the first parameter.
Pass validating classes and options in form: Class, booleanCanBeNull, booleanIsOptional
It has several predefined validating classes: Arg.ANY, Arg.STRING, Arg.NUMBER, Arg.DATE, Arg.OBJECT, Arg.ARRAY, Arg.BOOLEAN

I use it only in development code and strip it out in production. It can be effectively done with ant com.moxiecode.ant.tasks.PreProcessTask. That's why using this library is wrapped in #ifdef DEBUG /#endif construction:

  //#ifdef DEBUG
     Arg.testArguments(
          arguments,
          Arg.STRING
     );
  //#endif

Example 1

  //#ifdef DEBUG
            Arg.testArguments(
                        arguments,
                        Arg.STRING, true, //string or null
                        Arg.ARRAY, //array, not null
                        Arg.ANY, false, true //optional, any type but not null
            );
  //#endif   

Example 2

Creating custom validating class. Specify validating function and error message.

  //#ifdef DEBUG
   var MY_CLASS = Arg.Test.extend(function(obj){
      return obj instanceof MyNamespace.My_Class;
   }, 'MyNamespace.My_Class is expected');

   function doSomething( obj) {
       Arg.testArguments(
           arguments,
           MY_CLASS, true //My_Class or null
       );
       ...
   }
  //#endif   

Example 3

Extend existing validating class. It shows also how to generate specific error messages.

  //#ifdef DEBUG  
   var STRING_8_30 = Arg.STRING.extend(function(str){
            if (Arg.STRING.prototype.isValid.call(this, str)) {
                        if (str.length < 8) {
                                    this.message = 'must be 8 characters or longer';
                                    return false;
                        } else if (str.length > 30) {
                                    this.message = 'must be 30 characters or shorter';
                                    return false;
                        }
                        return true;
            }
            return false;
      });
  //#endif

Have fun!

Updated

Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.