CheckIt
Simple validation for node and the browser
The CheckIt library aims to be a lightweight, flexible, and thorough validation library,
targeting both Node.js and the browser.
CheckIt has a dependency of underscore.js
Getting Started
Creating a CheckIt object
var checkIt = new CheckIt(validate, [properties]);
The CheckIt object's constructor takes two arguments, validate
and properties
validate - object, required
The object an optional options
argument, which can contain several properties:
Custom validation functions, either overriding the existing validations or adding additional functions
Properties:
run
run(validations, callback)
Runs the CheckIt validation, takes two required arguments:
validations
callback
addError
Adds an error to the stack of errors for a particular validation item.
errors
errors([item])
Returns the errors for the
singleErrors
Sample Javascript Object to Validate
var example = {
'user' : 'Joe User',
'email' : 'joe@example.com',
'password' : '123',
'confirm_password' : '456'
};
Validations:
There are several ways to structure the validation object, to provide maximum flexibility
for various use cases.
Example 1: Simple Validation
new CheckIt(example).run({
'user' : ['required', 'alphaDash', 'maxLength:255'],
'email' : ['required', 'validEmail'],
'password' : ['required']
}, function() {
// Callback function with `this` bound to the current CheckIt validation object
});
Example 2: Simple Validation, with more options
new CheckIt(example).run({
'user' : {
label : 'User Name',
message : "The user isn't formatted properly, please try again",
validation : ['required', 'alphaDash']
}
'email' : ['required', 'validEmail']
'password' : ['required']
}, function() {
// Callback function with `this` bound to the current CheckIt validation object
});
Rather than the shorthand syntax of an array of validations, the value for an item in the
validation hash can be an object containing
Example 3: Validation with custom function
In this example, there is a custom function which is only called when the
new CheckIt(example).run([{
'name' : ['required', 'alphaDash', 'maxLength:255']
'email' : ['required', 'validEmail', 'maxLength:6']
'password' : ['required']
}, function(obj) {
// validation function, called with `this` bound to the current CheckIt validation object
}], function(){
// Callback function with `this` bound to the current CheckIt validation object
});
Example 4: Validation With Async
Often times, it is beneficial to run an async function, either as part of the callback,
or as a separate function call, after which the main callback is executed.
new CheckIt(example).run([{
'name' : ['required', 'alphaDash', 'maxLength:255']
'email' : ['required', 'validEmail', 'maxLength:6']
'password' : ['required']
}, {
'email' : {
async : true
label : 'Email Address'
message : 'The email address isn\'t unique for the server'
validation : function(obj, checkit) {
$.get('/username/unique/'+ obj.email, function() {
checkit.complete('email');
})
.error(function(){
checkit.addError('email', 'The email is already being used on the site');
checkit.complete('email');
});
}
}
}], function() {
// Callback function with `this` bound to the current CheckIt validation object
});
Example 5: Adding additional context to the validation object
new CheckIt(example).run({
'user' : {
validations : ['required', 'maxLength:255'],
formField : $("[name=user]")
},
function(err) {
if (err) {
_.each(err, function(key, value) {
this.validations[key].formField.find('.help-block').html(value.toString());
});
} else {
// Run Success!
}
}
})
Validation Failure
There are two ways to handle validation failure in custom callbacks.
The first is to throw an exception:
function(obj, checkit) {
if ( ! _.isArray(obj.item)) {
throw 'The item must be an array';
}
}
The second is to push an item onto the errors stack:
function(obj) {
if ( ! _.isArray(obj.item)) {
this.addError(item, 'The item must be an array')
}
}
Validation Types:
required
contains:value
matches
validEmail
validEmails
minLength
maxLength
exactLength
greaterThan
lessThan
alpha
alphaNumeric
alphaDash
numeric
integer
decimal
validIp
base64
natural
naturalNonZero
Underscore Methods:
include
any
has
isEmpty
isEqual
isElement
isArray
isObject
isArguments
isFunction
isString
isNumber
isFinite
isBoolean
isDate
isRegExp
isNaN
isNull
isUndefined
License
MIT License
Copyright (C) 2012 Tim Griesser <tim@tgriesser.com>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.