Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
m.validator = function (model, validations) {
this.errors = {}
this.validations = validations
this.model = model
}
m.validator.prototype.hasErrors = function () {
return Object.keys(this.errors).length
}
m.validator.prototype.hasError = function (prop) {
return this.errors[prop]
}
m.validator.prototype.clearErrors = function () {
this.errors = {}
}
m.validator.prototype.validate = function () {
this.clearErrors()
for( var key in this.validations ){
var validator = this.validations[key]
var value = this.model[key]()
var result = validator.bind(this.model)(value)
if (result) {
this.errors[key] = result
}
}
return self
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.