Sha256: f3ba72b2c2485fd9e4ec2b8360e69f8042cf86e8a2de0a0d7963c21f75f4a836

Contents?: true

Size: 1.63 KB

Versions: 1

Compression:

Stored size: 1.63 KB

Contents

# frozen_string_literal: true

module EasyParams
  # helpers for recursive validation
  module Validation
    private

    def validate_nested
      attributes.each_value do |value|
        case value
        when EasyParams::Types::StructsCollection
          value.each(&:valid?)
        when EasyParams::Types::Struct.class
          value.valid?
        end
      end
      attributes.each(&aggregate_nested_errors)
    end

    def aggregate_nested_errors
      proc do |attr_name, value, array_index, error_key_prefix|
        case value
        when EasyParams::Types::StructsCollection
          value.each.with_index do |element, i|
            aggregate_nested_errors[attr_name, element, "[#{i}]", error_key_prefix]
          end
        when EasyParams::Types::Struct.class
          handle_nested_errors(value, error_key_prefix, attr_name, array_index)
        end
      end
    end

    def handle_nested_errors(value, error_key_prefix, attr_name, array_index)
      return if value.errors.blank?

      error_key_components = [error_key_prefix, attr_name, array_index]
      attr_error_key_prefix = error_key_components.compact.join('.').gsub(/\.\[(\d+)\]/, '[\1]')
      add_errors_on_top_level(value, attr_error_key_prefix)
    end

    if defined? ActiveModel::Error
      def add_errors_on_top_level(value, attr_error_key_prefix)
        value.errors.each { |error| errors.add("#{attr_error_key_prefix}.#{error.attribute}", error.message) }
      end
    else
      def add_errors_on_top_level(value, attr_error_key_prefix)
        value.errors.each { |key, message| errors.add("#{attr_error_key_prefix}.#{key}", message) }
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
easy_params-0.5.0 lib/easy_params/validation.rb