Skip to content

Commit bafeaf9

Browse files
author
Ozer Chagatai
committed
Added functionality to given to allow it to pass in multiple arguments which will create nested scopes.
1 parent 90c3391 commit bafeaf9

File tree

4 files changed

+35
-4
lines changed

4 files changed

+35
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
* [#1227](https://github.com/ruby-grape/grape/pull/1227): Store `message_key` on Grape::Exceptions::Validation - [@stjhimy](https://github.com/sthimy).
77
* [#1232](https://github.com/ruby-grape/grape/pull/1232): Helpers are now available inside `rescue_from` - [@namusyaka](https://github.com/namusyaka).
8+
* [#1237](https://github.com/ruby-grape/grape/pull/1237): Allow multiple parameters in `given`, which behaves as if the scopes were nested in the inputted order - [@ochagata](https://github.com/ochagata).
89
* Your contribution here.
910

1011
#### Fixes

lib/grape/dsl/parameters.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,9 +165,11 @@ def all_or_none_of(*attrs)
165165
# @raise Grape::Exceptions::UnknownParameter if `attr` has not been
166166
# defined in this scope yet
167167
# @yield a parameter definition DSL
168-
def given(attr, &block)
169-
fail Grape::Exceptions::UnknownParameter.new(attr) unless declared_param?(attr)
170-
new_lateral_scope(dependent_on: attr, &block)
168+
def given(*attrs, &block)
169+
attrs.each do |attr|
170+
fail Grape::Exceptions::UnknownParameter.new(attr) unless declared_param?(attr)
171+
end
172+
new_lateral_scope(dependent_on: attrs, &block)
171173
end
172174

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

lib/grape/validations/params_scope.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ 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+
@dependent_on.each do |dependency|
40+
return false if params(parameters).try(:[], dependency).blank?
41+
end if @dependent_on
4042
return true if parent.nil?
4143
parent.should_validate?(parameters)
4244
end

spec/grape/validations/params_scope_spec.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,32 @@ def initialize(value)
298298
expect(last_response.status).to eq(200)
299299
end
300300

301+
it 'applies the validations of multiple parameters' do
302+
subject.params do
303+
optional :a, :b
304+
given :a, :b do
305+
requires :c
306+
end
307+
end
308+
subject.get('/multiple') { declared(params).to_json }
309+
310+
get '/multiple'
311+
expect(last_response.status).to eq(200)
312+
313+
get '/multiple', a: true
314+
expect(last_response.status).to eq(200)
315+
316+
get '/multiple', b: true
317+
expect(last_response.status).to eq(200)
318+
319+
get '/multiple', a: true, b: true
320+
expect(last_response.status).to eq(400)
321+
expect(last_response.body).to eq('c is missing')
322+
323+
get '/multiple', a: true, b: true, c: true
324+
expect(last_response.status).to eq(200)
325+
end
326+
301327
it 'raises an error if the dependent parameter was never specified' do
302328
expect do
303329
subject.params do

0 commit comments

Comments
 (0)