Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
* [#3083](https://github.com/ruby-grape/grape/pull/3083): Apply a mounting API's `default_error_status` to routes defined before the mount - [@ericproulx](https://github.com/ericproulx).
* [#3087](https://github.com/ruby-grape/grape/pull/3087): Let a mounted API `use` the named params of the scope it is mounted in - [@ericproulx](https://github.com/ericproulx).
* [#3093](https://github.com/ruby-grape/grape/pull/3093): Move the endpoint Rack stack building into `Endpoint::RackStack` to fix RuboCop - [@ericproulx](https://github.com/ericproulx).
* [#3099](https://github.com/ruby-grape/grape/pull/3099): Report a nested Array scope that got no Array under its own name only - [@ericproulx](https://github.com/ericproulx).
* Your contribution here.

### 4.1.0 (2026-10-31)
Expand Down
9 changes: 8 additions & 1 deletion lib/grape/validations/attributes_iterator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,14 @@ def do_each(params_to_process, tracker, index_scope, parent_indices, sent_elemen
# its elements deeper than that is yielded as-is, so the attribute
# validators see a non-hash and fail it the same way any other
# unexpected element type does.
if resource_params.is_a?(Array) && parent_indices.size < @max_nesting
if parent_indices.size < @max_nesting
# Short of that depth, a value is an Array scope's own param in an
# element of an Array further up. One that is not an Array was left
# out or given as something else, which that scope's own validators
# report; it has no elements, as #each finds at the top. Yielding it
# as one reported a second error, under the index of the Array above.
next unless resource_params.is_a?(Array)

nested_indices = [index] + parent_indices
do_each(resource_params, tracker, index_scope, nested_indices, sent_elements?(resource_params, nested_indices), &)
next
Expand Down
47 changes: 35 additions & 12 deletions spec/grape/validations_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,35 @@ def define_optional_using
end
end

context 'requires an Array nested in an Array, given something else' do
before do
subject.params do
requires :points, type: Array do
requires :location, type: Array do
requires :id, type: Integer
end
end
end
subject.post('/') { 'ok' }
end

it 'reports a value that is not an Array under its own name only' do
body = { points: [{ location: [{ id: 1 }] }, { location: [{ id: 1 }, { id: 2 }] }, { location: 0 }] }
post '/', body.to_json, 'CONTENT_TYPE' => 'application/json'

expect(last_response.status).to eq(400)
expect(last_response.body).to eq('points[2][location] is invalid')
end

it 'reports one left out under its own name only' do
body = { points: [{ location: [{ id: 1 }, { id: 2 }] }, {}] }
post '/', body.to_json, 'CONTENT_TYPE' => 'application/json'

expect(last_response.status).to eq(400)
expect(last_response.body).to eq('points[1][location] is missing')
end
end

context 'requires :all using Grape::Entity documentation' do
def define_requires_all
documentation = {
Expand Down Expand Up @@ -755,7 +784,7 @@ def validate_param!(attr_name, params)
# NOTE: with body parameters in json or XML or similar this
# should actually fail with: children[parents][name] is missing.
expect(last_response.status).to eq(400)
expect(last_response.body).to eq('children[1][parents] is missing, children[0][parents][1][name] is missing, children[0][parents][1][name] is empty')
expect(last_response.body).to eq('children[1][parents] is missing')
end

it 'errors when a parameter is not present in array within array' do
Expand Down Expand Up @@ -786,17 +815,11 @@ def validate_param!(attr_name, params)
# should actually return 200, since an empty array is valid.
get '/within_array', children: []
expect(last_response.status).to eq(400)
expect(last_response.body).to eq(
'children[0][name] is missing, ' \
'children[0][parents] is missing, ' \
'children[0][parents] is invalid, ' \
'children[0][parents][0][name] is missing, ' \
'children[0][parents][0][name] is empty'
)
expect(last_response.body).to eq('children[0][name] is missing, children[0][parents] is missing, children[0][parents] is invalid')

get '/within_array', children: [{ name: 'Jay' }]
expect(last_response.status).to eq(400)
expect(last_response.body).to eq('children[0][parents] is missing, children[0][parents][0][name] is missing, children[0][parents][0][name] is empty')
expect(last_response.body).to eq('children[0][parents] is missing')
end

it 'errors when param is not an Array' do
Expand Down Expand Up @@ -944,7 +967,7 @@ def validate_param!(attr_name, params)
expect(last_response.status).to eq(200)
put_with_json '/within_array', children: [{ name: 'Jay' }]
expect(last_response.status).to eq(400)
expect(last_response.body).to eq('children[0][parents] is missing, children[0][parents][0][name] is missing')
expect(last_response.body).to eq('children[0][parents] is missing')
end
end

Expand Down Expand Up @@ -1085,7 +1108,7 @@ def validate_param!(attr_name, params)
it 'does internal validations if the outer group is present' do
get '/nested_optional_group', items: [{ key: 'foo' }]
expect(last_response.status).to eq(400)
expect(last_response.body).to eq('items[0][required_subitems] is missing, items[0][required_subitems][0][value] is missing')
expect(last_response.body).to eq('items[0][required_subitems] is missing')

get '/nested_optional_group', items: [{ key: 'foo', required_subitems: [{ value: 'bar' }] }]
expect(last_response.status).to eq(200)
Expand All @@ -1105,7 +1128,7 @@ def validate_param!(attr_name, params)
it 'handles validation within arrays' do
get '/nested_optional_group', items: [{ key: 'foo' }]
expect(last_response.status).to eq(400)
expect(last_response.body).to eq('items[0][required_subitems] is missing, items[0][required_subitems][0][value] is missing')
expect(last_response.body).to eq('items[0][required_subitems] is missing')

get '/nested_optional_group', items: [{ key: 'foo', required_subitems: [{ value: 'bar' }] }]
expect(last_response.status).to eq(200)
Expand Down
Loading