Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Spec for Array types leaking index into subsequent Hash validations #1313

Merged
merged 1 commit into from
Aug 18, 2016
Merged
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
28 changes: 28 additions & 0 deletions spec/grape/validations_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down