Grape: Parameter dependecy using `given` does not work with nested arrays and custom block

Created on 23 Mar 2017  路  1Comment  路  Source: ruby-grape/grape

What's happening

Given I declared some rules to be dependent on other param using given parameter: -> v { v } and I did it inside a nested array param, it raises error.

Expected behavior

It should apply the rules.

Extended Explanation

Given this params:

~~~ruby
params do
optional :comments, type: Array do
optional :_destroy, coerce: Boolean
given _destroy: -> v { !v } do
requires :name
end
end
end

put('/asdf') do
declared(params)
end
~~~
When I call the controller with this params:

~json
{"comments": [{ "_destroy": true}]}
~

It fails with:

~~~ruby
TypeError (no implicit conversion of Symbol into Integer):

activesupport (5.0.1) lib/active_support/core_ext/object/try.rb:17:in []' activesupport (5.0.1) lib/active_support/core_ext/object/try.rb:17:inpublic_send'
activesupport (5.0.1) lib/active_support/core_ext/object/try.rb:17:in try!' activesupport (5.0.1) lib/active_support/core_ext/object/try.rb:6:intry'
grape (0.19.1) lib/grape/validations/params_scope.rb:49:in `block in should_validate?'
~~~

This is the source from grape where it fails:

~ruby
if dependency.is_a?(hash)
dependency_key = dependency.keys[0]
proc = dependency.values[0]
require 'pry'
binding.pry
return false unless proc.call(params(parameters).try(:[], dependency_key))
~

I have put a binding.pry here on the source to see what's happening.

~ruby
[1] pry(#)> params(parameters)
=> [{"_destroy"=>true}]
[3] pry(#)> dependency_key
=> :_destroy
~

The validations are being run over the entire array instead of over each element of it. It basically runs [{"_destroy"=>true}][:_destroy] which raises the TypeError (no implicit conversion of Symbol into Integer) error.

bug?

Most helpful comment

>All comments

Was this page helpful?
0 / 5 - 0 ratings