Class: EasyParams::Base

Inherits:
Dry::Struct
  • Object
show all
Includes:
ActiveModel::Validations
Defined in:
lib/easy_params/base.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.nameObject



7
8
9
# File 'lib/easy_params/base.rb', line 7

def self.name
  'EasyParams::Base'
end

Instance Method Details

#validate_nestedObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/easy_params/base.rb', line 19

def validate_nested
  run_nested_validations = proc do |attr_name, value, array_index, error_key_prefix|
    case value
    when Array
      value.each.with_index do |element, i|
        run_nested_validations[attr_name, element, i, error_key_prefix]
      end
    when self.class.struct
      if value.invalid?
        error_key_components = [error_key_prefix, attr_name, array_index]
        attr_error_key_prefix = error_key_components.compact.join('/')
        value.errors.each do |error_key, error_message|
          errors.add("#{attr_error_key_prefix}/#{error_key}", error_message)
        end
      end
      value.attributes.each do |nested_attr_name, nested_value|
        run_nested_validations[nested_attr_name, nested_value, nil, attr_error_key_prefix]
      end
    else
      # NOOP
    end
  end
  attributes.each(&run_nested_validations)
end