Exception: Coarnotify::ValidationError
- Inherits:
-
NotifyException
- Object
- StandardError
- NotifyException
- Coarnotify::ValidationError
- 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
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
Instance Method Summary collapse
-
#add_error(key, value) ⇒ Object
Record an error on the supplied key with the message value.
-
#add_nested_errors(key, subve) ⇒ Object
Take an existing ValidationError and add it as a nested set of errors under the supplied key.
-
#has_errors? ⇒ Boolean
Are there any errors registered.
-
#initialize(errors = {}) ⇒ ValidationError
constructor
Create a new ValidationError with the given errors hash.
-
#to_s ⇒ String
String representation of the errors.
Constructor Details
#initialize(errors = {}) ⇒ ValidationError
Create a new ValidationError with the given errors hash
65 66 67 68 |
# File 'lib/coarnotify/exceptions.rb', line 65 def initialize(errors = {}) super() @errors = errors end |
Instance Attribute Details
#errors ⇒ Object (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
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
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
95 96 97 |
# File 'lib/coarnotify/exceptions.rb', line 95 def has_errors? !@errors.empty? end |
#to_s ⇒ String
String representation of the errors
102 103 104 |
# File 'lib/coarnotify/exceptions.rb', line 102 def to_s @errors.to_s end |