Skip to content
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

Parameter dependecy using given does not work with nested arrays and custom block #1602

Closed
nicooga opened this issue Mar 23, 2017 · 1 comment
Labels

Comments

@nicooga
Copy link

nicooga commented Mar 23, 2017

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:

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:

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

It fails with:

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:in `public_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:in `try'
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:

          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.

[1] pry(#<Grape::Validations::ParamsScope>)> params(parameters)
=> [{"_destroy"=>true}]
[3] pry(#<Grape::Validations::ParamsScope>)> 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.

@dblock
Copy link
Member

dblock commented Jun 12, 2017

Closing via #1625

@dblock dblock closed this as completed Jun 12, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants