Skip to content

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

@nicooga

Description

@nicooga

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions