Class: HashyArrayValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
lib/hashy_validator/hashy_array_validator.rb

Instance Method Summary collapse

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/hashy_validator/hashy_array_validator.rb', line 4

def validate_each(record, attribute, value)
  instance_value = HashyValueValidator.new(value, options)

  unless instance_value.valid?
    record.errors.add(attribute, instance_value.reason)
    return false
  end

  unless instance_value.value.is_a?(Array)
    record.errors.add(attribute, :not_an_array)
    return false
  end

  value = instance_value.value

  # force all array entries to have string keys
  # discard keys that do not have validators
  value = value.map { |e| e.stringify_keys.slice(*instance_value.validations.keys) }

  # we validate each object in the array
  value.each do |t|
    # if boolean found as any of the validations we force value to boolean - if present
    instance_value.boolean_attrs.each do |boolean_attr|
      t[boolean_attr] = HashyValueValidator.get_boolean_value(t[boolean_attr]) if t.key?(boolean_attr)
    end

    # keep track of unique values and add error if needed
    instance_value.unique_attrs.each_key do |unique_attr|
      if instance_value.unique_attrs[unique_attr].include?(t[unique_attr])
        record.errors.add(attribute, "'#{unique_attr}' not unique")
      else
        instance_value.unique_attrs[unique_attr] << t[unique_attr]
      end
    end

    # use default hash validator
    validator = HashValidator.validate(t, instance_value.validations)
    unless validator.valid?
      validator.errors.each { |k, v| record.errors.add(attribute, "'#{k}' #{v}") }
    end
  end

  # update the value even if errors found
  # we use send write param so we also support attr_accessor attributes
  record.send("#{attribute}=", value)
end