checker

Checker is the collection of common abstract methods for validatiors and setters.

checker

Checker is the collection of common abstract node.js methods for validatiors and setters.

Usage

npm install checker --save
var checker = require('checker');

Synopsis

checker(schema, options).check(data, function(errvaluedetails){
});

The parsed object.

{
    <name>: <detail>
}
  • detail.value mixed the parsed value
  • detail.is_default Boolean if the current property is defined in schema, but the input data doesn't have it, then the value will be true
  • detail.is_cooked Boolean if there're any setters, it will be true
  • detail.origin the origin value of the property
  • detail.error the error belongs to the current property. If not exists, it will be null

Validation, Error Messages

var schema = {
    username: {
        validatorfunction(value){
            return /^[a-zA-Z0-9]{6,}$/.test(value);
        },
        message: 'Username must only contain letters, numbers; ' 
            + 'Username must contain at least 6 charactors'
    }
};
 
var c = checker(schema);
 
c.check({
    username: 'a'
}, function(err){
    if(err){
        console.log(err); // Then, `schema.username.message` will be displayed. 
    }
});

The error hint of the example above is bad, because we want to know the very certain reason why we are wrong.

The schema below is equivalent to the one of the previous section:

{
    validator: [
        function(value){
            return value && value.length > 5;
        }, 
        /^[a-zA-Z0-9]+$/
    ],
    message: [
        'Username must contain at least 6 charactors', 
        'Username must only contain letters and numbers'
    ];
}
{
    validatorfunction(value){
        var done = this.async();
        // this is an async method, and takes sooooo long... 
        remote_check(value, function(err){
            done(err); // `err` will pass to the `callback` 
        });
    }
}

Programmatical Details

Default error message

By default, checker will check each properties in series,

If options.limit is true and a certain property of the input data is not defined in the schema, the property will be removed.

Default to false.

By default, checker will exit immediately at the first error. But if options.check_all is true, it will parse all the properties, and collect every possible error.

See sections below.

{
    <name>: <rule>
}

Where rule might contains (all properties are optional):

  • RegExp The regular exp that input must matches against
  • Function Validation function. If arguments.length === 3, it will be considered as an async methods
  • Array.<RegExp|Function> Group of validations. Asks will check each validator one by one. If validation fails, the rest validators will be skipped.
  • See sections above for details

See sections above for details.

Default error message

Inside validators(rule.validator) and setters(rule.setter), there're several opaque methods

Generate the done function to make the validator or setter become an async method.

var done = this.async();

For details, see the demos above.

The value of the input object by name

Change the value of the specified property of the input object.

{
    username: {
    },
    
    password: {
        validator: function(value){
            var username = this.get('username');
            
            // Guests are welcome even without passwords
            return value || username === 'guest';
        }
    }
}

Notice that you'd better use this.get and this.set with the options.parallel setting as false(the default value). Otherwise, it might encounter unexpected situations, because the value of the object is ever changing due to the setter.

So, use them wisely.

The options.context itself.