Sha256: 0595078173dfa3ab52056922f9dc08e3cd45d5e8b361d9fd8b3d30fdf9a39cbd

Contents?: true

Size: 717 Bytes

Versions: 6

Compression:

Stored size: 717 Bytes

Contents

module Grape
  module Validations
    require 'grape/validations/validators/multiple_params_base'
    class MutualExclusionValidator < MultipleParamsBase
      attr_reader :processing_keys_in_common

      def validate!(params)
        super
        if two_or_more_exclusive_params_are_present
          fail Grape::Exceptions::Validation, params: processing_keys_in_common, message_key: :mutual_exclusion
        end
        params
      end

      private

      def two_or_more_exclusive_params_are_present
        scoped_params.any? do |resource_params|
          @processing_keys_in_common = keys_in_common(resource_params)
          @processing_keys_in_common.length > 1
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
grape-0.14.0 lib/grape/validations/validators/mutual_exclusion.rb
grape-0.13.0 lib/grape/validations/validators/mutual_exclusion.rb
grape-0.12.0 lib/grape/validations/validators/mutual_exclusion.rb
grape-0.11.0 lib/grape/validations/validators/mutual_exclusion.rb
grape-0.10.1 lib/grape/validations/validators/mutual_exclusion.rb
grape-0.10.0 lib/grape/validations/validators/mutual_exclusion.rb