diff --git a/CHANGELOG.md b/CHANGELOG.md index e5ea0c76e..a9ea93240 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/lib/grape/validations/attributes_iterator.rb b/lib/grape/validations/attributes_iterator.rb index 7339ba752..d4acc0779 100644 --- a/lib/grape/validations/attributes_iterator.rb +++ b/lib/grape/validations/attributes_iterator.rb @@ -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 diff --git a/spec/grape/validations_spec.rb b/spec/grape/validations_spec.rb index 842b58c93..a69d06568 100644 --- a/spec/grape/validations_spec.rb +++ b/spec/grape/validations_spec.rb @@ -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 = { @@ -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 @@ -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 @@ -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 @@ -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) @@ -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)