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.
It should apply the rules.
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(#
=> [{"_destroy"=>true}]
[3] pry(#
=> :_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.
Closing via https://github.com/ruby-grape/grape/pull/1625
Most helpful comment
Closing via https://github.com/ruby-grape/grape/pull/1625