Skip to content

Instantly share code, notes, and snippets.

@syntruth
Last active August 29, 2015 14:08
Show Gist options
  • Save syntruth/227e3fdddda9c27d69ff to your computer and use it in GitHub Desktop.
Save syntruth/227e3fdddda9c27d69ff to your computer and use it in GitHub Desktop.
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