Skip to content

Instantly share code, notes, and snippets.

@JoeChapman
Created February 24, 2012 14:22
Show Gist options
  • Save JoeChapman/1901220 to your computer and use it in GitHub Desktop.
Save JoeChapman/1901220 to your computer and use it in GitHub Desktop.
validator object, compares user input (data) against config object and fires callbacks on result
/**
* @author: joseph chapman
* @email
* @dependencies: sky
* @name: validator
* @dependencies validator.types and validator.config
*/
(function($, window, validator, undefined) {
validator = {
types: {},
config: {},
messages: [],
validate: function(data) {
var i, checker, type, isValid;
for (i in data) {
if (data.hasOwnProperty(i)) {
type = this.config[i];
checker = this.types[type];
if (!type) {
continue;
}
if (!checker) {
throw {
name: "ValidationError",
message: "No handler to validate type" + type
};
}
isValid = checker.validate(data[i]);
if (!isValid) {
if (typeof checker.errorHandler === 'function') {
checker.errorHandler();
}
msg = checker.instructions;
this.messages.push(msg);
}
}
}
return checker.validate();
}
};
}(jQuery, window, (validator || {})));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment