diff --git a/lib/grape/dsl/parameters.rb b/lib/grape/dsl/parameters.rb index e12f14c954..adbad5d2ba 100644 --- a/lib/grape/dsl/parameters.rb +++ b/lib/grape/dsl/parameters.rb @@ -168,8 +168,8 @@ def all_or_none_of(*attrs) def given(*attrs, &block) attrs.each do |attr| fail Grape::Exceptions::UnknownParameter.new(attr) unless declared_param?(attr) - new_lateral_scope(dependent_on: attr, &block) end + new_lateral_scope(dependent_on: attrs, &block) end # Test for whether a certain parameter has been defined in this params diff --git a/lib/grape/validations/params_scope.rb b/lib/grape/validations/params_scope.rb index 431d57c995..d498129417 100644 --- a/lib/grape/validations/params_scope.rb +++ b/lib/grape/validations/params_scope.rb @@ -36,7 +36,11 @@ def initialize(opts, &block) # validated def should_validate?(parameters) return false if @optional && params(parameters).respond_to?(:all?) && params(parameters).all?(&:blank?) - return false if @dependent_on && params(parameters).try(:[], @dependent_on).blank? + if @dependent_on + @dependent_on.each do |dependency| + return false if params(parameters).try(:[], dependency).blank? + end + end return true if parent.nil? parent.should_validate?(parameters) end diff --git a/spec/grape/validations/params_scope_spec.rb b/spec/grape/validations/params_scope_spec.rb index caa1c7f304..813778d32b 100644 --- a/spec/grape/validations/params_scope_spec.rb +++ b/spec/grape/validations/params_scope_spec.rb @@ -278,8 +278,7 @@ def initialize(value) context 'when validations are dependent on a parameter' do before do subject.params do - optional :a - optional :b + optional :a, :b given :a, :b do requires :c end @@ -291,7 +290,7 @@ def initialize(value) get '/test' expect(last_response.status).to eq(200) - get '/test', a: true + get '/test', a: true, b: true expect(last_response.status).to eq(400) expect(last_response.body).to eq('c is missing')