Simple validation library for Javascript models. It doesn't touch the DOM, it doesn't return messages. It just takes an object and validates it against a set of rules returning an object for working with errors.

I wasn't happy with the validation libraries available for Backbone models. Most are large and try and do too much. Ok was designed to take your models attributes and return a nice error object that your views can use to update your UI. It doesn't attempt to cover every validation use-case but it provides a simple interface for extending the validation rules available.

The schema is just a simple object with keys that map to attributes. The values are a hash of rules. The key is the validation type and the value will be passed to the validation method in OK.Validator. For example

var schema ={

name:{

string:true

}

};

This object can be passed to OK.validate(attributes, schema) to be used once or you can contruct a new OK instance new OK(schema)
and reuse the same schema validation across your application.

Loop through the errors for an attribute and call the callback. If the attr param is omitted and a function is passed in as the
only parameter it will loop through the errors object itself so you can check each of the attributes.

Adding new validation rules is just a matter of adding a new method to the prototype of OK.Validator

OK.Validator.prototype.creditcard=function(value, options, data){

return CreditCard.check(value, options);

};

The function will be passed the value, the options for that rule (which is whatever you've defined in your schema) and the object of all the data that is being validated so you can check the value in relation to other values. Here's an example schema using the validation method we just defined:

var schema ={

card:{

creditcard:{

type:'mastercard'

}

}

};

var data ={

card:'foo'

};

OK.validate(data, schema);

In the validtion method, value will be 'foo', options will be { type: mastercard } and data will be the object that is passed in to validate.