Skip to content

Commit c8be0b0

Browse files
author
Ozer Chagatai
committed
modified it so it doesn't create a new scope for each input
1 parent 7a3af1c commit c8be0b0

File tree

3 files changed

+8
-5
lines changed

3 files changed

+8
-5
lines changed

lib/grape/dsl/parameters.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,8 @@ def all_or_none_of(*attrs)
168168
def given(*attrs, &block)
169169
attrs.each do |attr|
170170
fail Grape::Exceptions::UnknownParameter.new(attr) unless declared_param?(attr)
171-
new_lateral_scope(dependent_on: attr, &block)
172171
end
172+
new_lateral_scope(dependent_on: attrs, &block)
173173
end
174174

175175
# Test for whether a certain parameter has been defined in this params

lib/grape/validations/params_scope.rb

+5-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,11 @@ def initialize(opts, &block)
3636
# validated
3737
def should_validate?(parameters)
3838
return false if @optional && params(parameters).respond_to?(:all?) && params(parameters).all?(&:blank?)
39-
return false if @dependent_on && params(parameters).try(:[], @dependent_on).blank?
39+
if @dependent_on
40+
@dependent_on.each do |dependency|
41+
return false if params(parameters).try(:[], dependency).blank?
42+
end
43+
end
4044
return true if parent.nil?
4145
parent.should_validate?(parameters)
4246
end

spec/grape/validations/params_scope_spec.rb

+2-3
Original file line numberDiff line numberDiff line change
@@ -278,8 +278,7 @@ def initialize(value)
278278
context 'when validations are dependent on a parameter' do
279279
before do
280280
subject.params do
281-
optional :a
282-
optional :b
281+
optional :a, :b
283282
given :a, :b do
284283
requires :c
285284
end
@@ -291,7 +290,7 @@ def initialize(value)
291290
get '/test'
292291
expect(last_response.status).to eq(200)
293292

294-
get '/test', a: true
293+
get '/test', a: true, b: true
295294
expect(last_response.status).to eq(400)
296295
expect(last_response.body).to eq('c is missing')
297296

0 commit comments

Comments
 (0)