-
-
Save goliatone/a282a9c4b70a012d8ab5 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
function Validator () { | |
this.errors = []; | |
this.decoratorsList = []; | |
} | |
Validator.prototype.decorate = function(name, args) { | |
this.decoratorsList.push({ name: name, args: args }); | |
}; | |
Validator.decorators = {}; | |
Validator.decorators.hasName = { | |
validate: function(form, args) { | |
// Code to verify presence of name... | |
this.errors.push('no name!'); | |
} | |
}; | |
Validator.decorators.hasAge = { | |
validate: function(form, args) { | |
// Code to verify presence of age... | |
this.errors.push('no age!'); | |
} | |
}; | |
Validator.decorators.hasZipCode = { | |
validate: function(form, args) { | |
// Code to verify presence of zip code... | |
this.errors.push('no zip!'); | |
} | |
}; | |
Validator.prototype.validate = function(form) { | |
var i, | |
max, | |
temp, | |
name, | |
args; | |
this.form = form; | |
max = this.decoratorsList.length; | |
for (i = 0; i < max; i++) { | |
temp = this.decoratorsList[i]; | |
name = temp.name; | |
args = temp.args; | |
Validator.decorators[name].validate.call(this, form, args); | |
}; | |
}; | |
// Try it out! | |
var validator = new Validator(); | |
validator.decorate('hasName', { length: 5 }); | |
validator.decorate('hasAge', { minimum: 21 }); | |
validator.decorate('hasZipCode'); | |
validator.validate({}); // some form data. in this case just an anonymous object | |
console.log(validator.errors); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment