Basics

ValidJS abstracts away the tedium of value validation. To start, let's see some simple type checking.

function build(x, y, z, name, power) {

   var errors;

   if (ns.validate(x, "int",
                   y, "int",
                   z, "int",
                   name, "string",
                   power, "float",
                   errors)) {

      return {location: [x, y, z],
              name: name,
              power: power};

   }

   else {

      return null;

   }

}

Here, the variables x, y, and z must be integers, name must be a string, and power must be a float. A lot of validation tends to revolve around data types, but we can get finer grained than that.

function alterTemp(deltaDegrees, user, location) {

    var errors;

    if (ns.validate(
        deltaDegrees, ["int", ["between", -5, 5]],
        user, ["string", ["lenBetween", 5, 30]],
        location, "string",
        errors)) {

        temperature += deltaDegrees;
        alert(user + " changed the temperature to " + temperature + ".");

    }

}

Not only must deltaDegrees be an integer, it must also be at least -5 and at most 5. User must be a string, but also have a length between 5 and 30. Location simply needs to be a string.

There are a couple of things to notice here. Whenever you specify more than one validator ("int", "lenBetween", etc.), you must wrap them in an array. Also, if the validator takes arguments, such as "between" or "lenBetween", that also necessitates an array.

Validators will be executed in the order written, so you don't have to worry about "between" throwing a fit when deltaDegrees turns out to be a string. For that reason alone, you should prefer putting data type validators first in the list.

Duck Validation

This is my favorite part. You know how JavaScript lets you create objects on-the-fly and add to them whenever? Wouldn't it be nice to check to see if certain properties exist on an object before manipulating it?

function manipulate(user) {

    var errors;

    if (ns.validate(user, [["duck",
        {
            mustHave: [ "name", "occupation", "gpa" ],
            ensureThat: {
                gpa: ["float", ["between", 3.5, 4.0]],
                bankAccount: {
                    checking: ["int", ["greaterThan", 100]]
                },
                "rothIRA.stocks": ["array", ["lengthAtLeast", 2]],
                "rothIRA.bonds": {
                    hasEmergingMarkets: ["true"]
                }
            }
        }]], errors)) {

        // do stuff
    }

}

someGuy = {
    name: "Rutherford",
    occupation: "Presidentz",
    gpa: 3.56,
    yearGraduated: 1847,

    bankAccount: {
        checking: 2506,
        savings: 9231
    },

    rothIRA: {
        stocks: [ "NGVC", "IJH", "IVV", "IDV", "DVY" ],
        bonds: {
            securities: ["EMB"],
            hasEmergingMarkets: true,
            hasDomestic: false
        }
    }
};

manipulate(someGuy);

Nice.

 

Last edit: James Brawn, Jr. 2013-04-25