Skip to content

Commit 11fd3dd

Browse files
committed
Fixed edge case regarding Hash with inner param
1 parent 60720fc commit 11fd3dd

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#### Fixes
1111

12+
* [#1156](https://github.com/ruby-grape/grape/pull/1156): Fixed `no implicit conversion of Symbol into Integer` with nested `values` validation - [@quickpay](https://github.com/quickpay).
1213
* [#1153](https://github.com/ruby-grape/grape/pull/1153): Fixes boolean declaration in an external file - [@towanda](https://github.com/towanda).
1314
* [#1142](https://github.com/ruby-grape/grape/pull/1142): Makes #declared unavailable to before filters - [@jrforrest](https://github.com/jrforrest).
1415
* [#1114](https://github.com/ruby-grape/grape/pull/1114): Fix regression which broke identical endpoints with different versions - [@suan](https://github.com/suan).

lib/grape/validations/validators/values.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ def initialize(attrs, options, required, scope)
77
end
88

99
def validate_param!(attr_name, params)
10+
return unless params.is_a?(Hash)
1011
return unless params[attr_name] || required_for_root_scope?
1112

1213
values = @values.is_a?(Proc) ? @values.call : @values

spec/grape/validations_spec.rb

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,35 @@ def define_requires_none
318318
end
319319
end
320320

321+
context 'hash with a required param with validation' do
322+
before do
323+
subject.params do
324+
requires :items, type: Hash do
325+
requires :key, type: String, values: %w(a b)
326+
end
327+
end
328+
subject.get '/required' do
329+
'required works'
330+
end
331+
end
332+
333+
it 'errors when param is not a Hash' do
334+
get '/required', items: 'not a hash'
335+
expect(last_response.status).to eq(400)
336+
expect(last_response.body).to eq('items is invalid, items[key] is missing, items[key] is invalid')
337+
338+
get '/required', items: [{ key: 'hash in array' }]
339+
expect(last_response.status).to eq(400)
340+
expect(last_response.body).to eq('items is invalid, items[key] does not have a valid value')
341+
end
342+
343+
it 'works when all params match' do
344+
get '/required', items: { key: 'a' }
345+
expect(last_response.status).to eq(200)
346+
expect(last_response.body).to eq('required works')
347+
end
348+
end
349+
321350
context 'group' do
322351
before do
323352
subject.params do

0 commit comments

Comments
 (0)