Last active
August 29, 2015 14:08
-
-
Save syntruth/227e3fdddda9c27d69ff to your computer and use it in GitHub Desktop.
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
class ValidationErrors | |
constructor: () -> | |
this.reset() | |
return | |
reset: () -> | |
@errors = {} | |
return this | |
set: (key, msg = '') -> | |
@errors[key] = msg if key and msg | |
return this | |
unset: (key) -> | |
value = @errors[key] | |
delete @errors[key] | |
return value | |
keys: () -> | |
arr = [] | |
this.eachError (key) -> arr.push key | |
return arr | |
size: () -> | |
n = 0 | |
this.eachError () -> n += 1 | |
return n | |
allGood: () -> if this.size() is 0 then true else false | |
eachError: (callback) -> | |
return false unless typeof callback is 'function' | |
callback key, value for own key, value of @errors when key | |
return this | |
if typeof module isnt 'undefined' and module.exports | |
module.exports = ValidationErrors | |
else | |
this.ValidationErrors = ValidationErrors |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment