Skip to content

Commit

Permalink
modified it so it doesn't create a new scope for each input
Browse files Browse the repository at this point in the history
  • Loading branch information
Ozer Chagatai committed Jan 5, 2016
1 parent 7a3af1c commit c8be0b0
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 5 deletions.
2 changes: 1 addition & 1 deletion lib/grape/dsl/parameters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion lib/grape/validations/params_scope.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 2 additions & 3 deletions spec/grape/validations/params_scope_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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')

Expand Down

0 comments on commit c8be0b0

Please sign in to comment.