Skip to content

Changed the input of given #1237

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

* [#1227](https://github.com/ruby-grape/grape/pull/1227): Store `message_key` on Grape::Exceptions::Validation - [@stjhimy](https://github.com/sthimy).
* [#1232](https://github.com/ruby-grape/grape/pull/1232): Helpers are now available inside `rescue_from` - [@namusyaka](https://github.com/namusyaka).
* [#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).
* Your contribution here.

#### Fixes
Expand Down
8 changes: 5 additions & 3 deletions lib/grape/dsl/parameters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,11 @@ def all_or_none_of(*attrs)
# @raise Grape::Exceptions::UnknownParameter if `attr` has not been
# defined in this scope yet
# @yield a parameter definition DSL
def given(attr, &block)
fail Grape::Exceptions::UnknownParameter.new(attr) unless declared_param?(attr)
new_lateral_scope(dependent_on: attr, &block)
def given(*attrs, &block)
attrs.each do |attr|
fail Grape::Exceptions::UnknownParameter.new(attr) unless declared_param?(attr)
end
new_lateral_scope(dependent_on: attrs, &block)
end

# Test for whether a certain parameter has been defined in this params
Expand Down
4 changes: 3 additions & 1 deletion lib/grape/validations/params_scope.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ 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?
@dependent_on.each do |dependency|
return false if params(parameters).try(:[], dependency).blank?
end if @dependent_on
return true if parent.nil?
parent.should_validate?(parameters)
end
Expand Down
26 changes: 26 additions & 0 deletions spec/grape/validations/params_scope_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,32 @@ def initialize(value)
expect(last_response.status).to eq(200)
end

it 'applies the validations of multiple parameters' do
subject.params do
optional :a, :b
given :a, :b do
requires :c
end
end
subject.get('/multiple') { declared(params).to_json }

get '/multiple'
expect(last_response.status).to eq(200)

get '/multiple', a: true
expect(last_response.status).to eq(200)

get '/multiple', b: true
expect(last_response.status).to eq(200)

get '/multiple', a: true, b: true
expect(last_response.status).to eq(400)
expect(last_response.body).to eq('c is missing')

get '/multiple', a: true, b: true, c: true
expect(last_response.status).to eq(200)
end

it 'raises an error if the dependent parameter was never specified' do
expect do
subject.params do
Expand Down