Sha256: cd21c1d634d35f459d9e888b918dc17f0f78e936a90a3dc193f831723a08dd8a

Contents?: true

Size: 869 Bytes

Versions: 15

Compression:

Stored size: 869 Bytes

Contents

module Grape
  module Validations
    require 'grape/validations/validators/mutual_exclusion'
    class ExactlyOneOfValidator < MutualExclusionValidator
      def validate!(params)
        super
        if scope_requires_params && none_of_restricted_params_is_present
          raise Grape::Exceptions::Validation, params: all_keys, message: message(:exactly_one)
        end
        params
      end

      def message(default_key = nil)
        options = instance_variable_get(:@option)
        if options_key?(:message)
          (options_key?(default_key, options[:message]) ? options[:message][default_key] : options[:message])
        else
          default_key
        end
      end

      private

      def none_of_restricted_params_is_present
        scoped_params.any? { |resource_params| keys_in_common(resource_params).empty? }
      end
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
grape-1.2.4 lib/grape/validations/validators/exactly_one_of.rb
grape-1.2.3 lib/grape/validations/validators/exactly_one_of.rb
grape-1.2.2 lib/grape/validations/validators/exactly_one_of.rb
grape-1.2.1 lib/grape/validations/validators/exactly_one_of.rb
grape-1.2.0 lib/grape/validations/validators/exactly_one_of.rb
grape-1.1.0 lib/grape/validations/validators/exactly_one_of.rb
grape-1.0.3 lib/grape/validations/validators/exactly_one_of.rb
grape-1.0.2 lib/grape/validations/validators/exactly_one_of.rb
grape-1.0.1 lib/grape/validations/validators/exactly_one_of.rb
grape-1.0.0 lib/grape/validations/validators/exactly_one_of.rb
grape-0.19.2 lib/grape/validations/validators/exactly_one_of.rb
grape-0.19.1 lib/grape/validations/validators/exactly_one_of.rb
grape-0.19.0 lib/grape/validations/validators/exactly_one_of.rb
grape-0.18.0 lib/grape/validations/validators/exactly_one_of.rb
grape-0.17.0 lib/grape/validations/validators/exactly_one_of.rb