diff --git a/spec/grape/validations_spec.rb b/spec/grape/validations_spec.rb index 731d803fde..f3898bc05b 100644 --- a/spec/grape/validations_spec.rb +++ b/spec/grape/validations_spec.rb @@ -274,6 +274,28 @@ def define_requires_none end end + # Ensure there is no leakage between declared Array types and + # subsequent Hash types + context 'required with an Array and a Hash block' do + before do + subject.params do + requires :cats, type: Array[String], default: [] + requires :items, type: Hash do + requires :key + end + end + subject.get '/required' do + 'required works' + end + end + + it 'does not output index [0] for Hash types' do + get '/required', cats: ['Garfield'], items: { foo: 'bar' } + expect(last_response.status).to eq(400) + expect(last_response.body).to eq('items[key] is missing') + end + end + context 'required with a Hash block' do before do subject.params do @@ -292,6 +314,12 @@ def define_requires_none expect(last_response.body).to eq('items is missing, items[key] is missing') end + it 'errors when nested param not present' do + get '/required', items: { foo: "bar" } + expect(last_response.status).to eq(400) + expect(last_response.body).to eq('items[key] is missing') + end + it 'errors when param is not a Hash' do get '/required', items: 'hello' expect(last_response.status).to eq(400)