Sha256: ccbf18aafa1b81739942677b3c87b3b52198df4ad4c2bab82bf19fcdf56f11ed

Contents?: true

Size: 868 Bytes

Versions: 3

Compression:

Stored size: 868 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
          fail 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

3 entries across 3 versions & 1 rubygems

Version Path
grape-0.16.2 lib/grape/validations/validators/exactly_one_of.rb
grape-0.16.1 lib/grape/validations/validators/exactly_one_of.rb
grape-0.15.0 lib/grape/validations/validators/exactly_one_of.rb