Created
February 24, 2012 14:22
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @author: joseph chapman | |
* @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