Sha256: 8b98887c14c5569043a07484beab8ebf635e5a2724a4e29c1b5d5dd7b6fe6d99

Contents?: true

Size: 1.07 KB

Versions: 2

Compression:

Stored size: 1.07 KB

Contents

require_relative 'extract_errors'

module Voom
  module Commands
    module AggregateValidations
      include Commands::ExtractErrors
      # This method will combine all parameter validation errors into a single error hash
      # Warning: If the same key exists in two validations the last one wins
      # You pass it a lambda that validates parameters.
      # EXAMPLE:
      # errors = validate_workflows(-> {@params = validate_params(params_)},
      #                             -> {@update_location_wf = update_location_wf.new(params_)})
      #
      def aggregate_validations(*lambdas)
        lambdas.reduce({}) do |errors, lambda|
          begin
            lambda.call
          rescue Errors::ParameterValidation => e
            errors.merge!(extract_errors(e))
          end
          errors
        end
      end

      def aggregate_validations!(*lambdas)
        errors = aggregate_validations(*lambdas)
        trace {errors.inspect} if errors.any?
        raise Errors::ParameterValidation.new('Form validation failed.', errors) if errors.any?
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
voom-commands-0.1.1 lib/voom/commands/aggregate_validations.rb
voom-commands-0.1.0 lib/voom/commands/aggregate_validations.rb