Class: JsonAttributeValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
lib/trax/validators/json_attribute_validator.rb

Overview

validates each json property and bubbles up any errors also throws a generic can not be blank error, in the event that a hash is not provided

Instance Method Summary collapse

Instance Method Details

#validate_each(object, attribute, value) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/trax/validators/json_attribute_validator.rb', line 5

def validate_each(object, attribute, value)
  json_attribute = object.class.fields_module[attribute]

  value.instance_variable_set("@record", object)

  unless value.is_a?(json_attribute) && value.valid?
    if value.is_a?(json_attribute)
      value.errors.messages.each_pair do |k,v|
        v = v.flatten.join(", ") if v.is_a?(Array)
        object.errors.add("#{attribute}.#{k}", v)
      end
    else
      object.errors[attribute] << "can not be blank"
    end
  end
end