Skip to content

fix bug when validating within array parameters#495

Closed
asross wants to merge 2 commits intoruby-grape:masterfrom
asross:fix-array-params
Closed

fix bug when validating within array parameters#495
asross wants to merge 2 commits intoruby-grape:masterfrom
asross:fix-array-params

Conversation

@asross
Copy link
Copy Markdown
Contributor

@asross asross commented Oct 26, 2013

#333 introduced functionality to validate arrays. The case it was tested against is this:

params do
  group :tags do
    requires :name
  end
end

{ tags: [name: "ruby", name: "python"] } will pass validation, and { tags: [foo: "bar"] } will fail with a 400.

However, if you have a more complicated case, where you have new parameter scopes within each element of the array, grape throws an ArgumentError because ParamsScope#params expects to be receiving a hash (and #should_validate? also behaves unexpectedly). E.g.:

params do
  group :cats do
     requires :name
     group :owner do
        requires :name
     end
  end
end

You will get a 500 rather than a validation error or success when you try to submit:

{ 
  cats: [
    { name: 'Garfield', owner: { name: 'Arbuckle' } }, 
    { ...more cats... }
  ]
}

This pull request fixes that issue (although I'm not entirely happy with the deep nesting in #params) and, more importantly, adds a bunch of tests.

@dblock
Copy link
Copy Markdown
Member

dblock commented Oct 26, 2013

Check Travis, there're a few Rubocop violations (style). Update CHANGELOG. Amend/squash the PR with the changes, please. Thx.

So that `params([{ :foo => :baz }, { :foo => :bat }])` for `@element=:foo` returns `[:baz, :bat]` instead of an ArgumentError.
@dblock
Copy link
Copy Markdown
Member

dblock commented Oct 28, 2013

Merged via 3ce41ef.

@dblock dblock closed this Oct 28, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants