Exception: Coarnotify::ValidationError

Inherits:
NotifyException show all
Defined in:
lib/coarnotify/exceptions.rb

Overview

Exception class for validation errors.

This class is designed to be thrown and caught and to collect validation errors as it passes through the validation pipeline.

For example an object validator may do something like this:

def validate
  ve = ValidationError.new
  ve.add_error(prop_name, "#{prop_name} is a required field")
  raise ve if ve.has_errors?
  true
end

If this is called by a subclass which is also validating, then this may be used like this:

def validate
  ve = ValidationError.new
  begin
    super
  rescue ValidationError => superve
    ve = superve
  end

  ve.add_error(prop_name, "#{prop_name} is a required field")
  raise ve if ve.has_errors?
  true
end

By the time the ValidationError is finally raised to the top, it will contain all the validation errors from the various levels of validation that have been performed.

The errors are stored as a multi-level hash with the keys at the top level being the fields in the data structure which have errors, and within the value for each key there are two possible keys:

  • errors: an array of error messages for this field

  • nested: a hash of further errors for nested fields

    {

    "key1" => {
      "errors" => ["error1", "error2"],
      "nested" => {
        "key2" => {
          "errors" => ["error3"]
        }
      }
    }
    

    }

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(errors = {}) ⇒ ValidationError

Create a new ValidationError with the given errors hash

Parameters:

  • errors (Hash) (defaults to: {})

    The errors hash to initialize with



65
66
67
68
# File 'lib/coarnotify/exceptions.rb', line 65

def initialize(errors = {})
  super()
  @errors = errors
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



60
61
62
# File 'lib/coarnotify/exceptions.rb', line 60

def errors
  @errors
end

Instance Method Details

#add_error(key, value) ⇒ Object

Record an error on the supplied key with the message value

Parameters:

  • key (String)

    the key for which an error is to be recorded

  • value (String)

    the error message



74
75
76
77
# File 'lib/coarnotify/exceptions.rb', line 74

def add_error(key, value)
  @errors[key] ||= { "errors" => [] }
  @errors[key]["errors"] << value
end

#add_nested_errors(key, subve) ⇒ Object

Take an existing ValidationError and add it as a nested set of errors under the supplied key

Parameters:

  • key (String)

    the key under which all the nested validation errors should go

  • subve (ValidationError)

    the existing ValidationError object



83
84
85
86
87
88
89
90
# File 'lib/coarnotify/exceptions.rb', line 83

def add_nested_errors(key, subve)
  @errors[key] ||= { "errors" => [] }
  @errors[key]["nested"] ||= {}

  subve.errors.each do |k, v|
    @errors[key]["nested"][k] = v
  end
end

#has_errors?Boolean

Are there any errors registered

Returns:

  • (Boolean)

    true if there are errors, false otherwise



95
96
97
# File 'lib/coarnotify/exceptions.rb', line 95

def has_errors?
  !@errors.empty?
end

#to_sString

String representation of the errors

Returns:

  • (String)

    string representation of the errors hash



102
103
104
# File 'lib/coarnotify/exceptions.rb', line 102

def to_s
  @errors.to_s
end