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

Bug - Coercion and Lambda Errors #1493

Merged
merged 3 commits into from
Sep 20, 2016
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 @@ -11,6 +11,7 @@

* [#1479](https://github.com/ruby-grape/grape/pull/1479): Support inserting middleware before/after anonymous classes in the middleware stack - [@rosa](https://github.com/rosa).
* [#1488](https://github.com/ruby-grape/grape/pull/1488): Ensure calling before filters when receiving OPTIONS request - [@namusyaka](https://github.com/namusyaka), [@jlfaber](https://github.com/jlfaber).
* [#1493](https://github.com/ruby-grape/grape/pull/1493): Coercion and lambda fails params validation - [@jonmchan](https://github.com/jonmchan).

0.17.0 (7/29/2016)
==================
Expand Down
4 changes: 4 additions & 0 deletions lib/grape/validations/params_scope.rb
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,10 @@ def validate(type, options, attrs, doc_attrs)
def validate_value_coercion(coerce_type, values)
return unless coerce_type && values
return if values.is_a?(Proc)
if values.is_a?(Hash)
return if values[:value] && values[:value].is_a?(Proc)
return if values[:except] && values[:except].is_a?(Proc)
end
coerce_type = coerce_type.first if coerce_type.is_a?(Array)
value_types = values.is_a?(Range) ? [values.begin, values.end] : values
if coerce_type == Virtus::Attribute::Boolean
Expand Down
30 changes: 30 additions & 0 deletions spec/grape/validations/params_scope_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,36 @@ def initialize(value)
end
end

context 'coercing values validation with proc' do
it 'allows the proc to pass validation without checking' do
subject.params { requires :numbers, type: Integer, values: -> { [0, 1, 2] } }

subject.post('/required') { 'coercion with proc works' }
post '/required', numbers: '1'
expect(last_response.status).to eq(201)
expect(last_response.body).to eq('coercion with proc works')
end

it 'allows the proc to pass validation without checking in value' do
subject.params { requires :numbers, type: Integer, values: { value: -> { [0, 1, 2] } } }

subject.post('/required') { 'coercion with proc works' }
post '/required', numbers: '1'
expect(last_response.status).to eq(201)
expect(last_response.body).to eq('coercion with proc works')
end

it 'allows the proc to pass validation without checking in except' do
subject.params { requires :numbers, type: Integer, values: { except: -> { [0, 1, 2] } } }

subject.post('/required') { 'coercion with proc works' }
post '/required', numbers: '10'
p last_response.body
expect(last_response.status).to eq(201)
expect(last_response.body).to eq('coercion with proc works')
end
end

context 'with range values' do
context "when left range endpoint isn't #kind_of? the type" do
it 'raises exception' do
Expand Down
57 changes: 57 additions & 0 deletions spec/grape/validations/validators/values_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ class API < Grape::API
{ type: params[:type] }
end

params do
requires :type, values: { except: -> { ValuesModel.excepts }, except_message: 'value is on exclusions list' }
end
get '/exclude/lambda/exclude_message' do
{ type: params[:type] }
end

params do
requires :type, values: { except: ValuesModel.excepts, message: 'default exclude message' }
end
Expand Down Expand Up @@ -132,6 +139,20 @@ class API < Grape::API
{ type: params[:type] }
end

params do
requires :type, values: { except: -> { ValuesModel.excepts } }
end
get '/except/exclusive/lambda' do
{ type: params[:type] }
end

params do
requires :type, type: Integer, values: { except: -> { [3, 4, 5] } }
end
get '/except/exclusive/lambda/coercion' do
{ type: params[:type] }
end

params do
requires :type, type: Integer, values: { value: 1..5, except: [3] }
end
Expand Down Expand Up @@ -182,6 +203,14 @@ def app
end
end

context 'with a custom exclude validation message' do
it 'does not allow an invalid value for a parameter' do
get('/custom_message/exclude/lambda/exclude_message', type: 'invalid-type1')
expect(last_response.status).to eq 400
expect(last_response.body).to eq({ error: 'type value is on exclusions list' }.to_json)
end
end

context 'exclude with a standard custom validation message' do
it 'does not allow an invalid value for a parameter' do
get('/custom_message/exclude/fallback_message', type: 'invalid-type1')
Expand Down Expand Up @@ -391,6 +420,34 @@ def app
end
end

context 'exclusive excepts with lambda' do
it 'allows any other value outside excepts' do
get '/except/exclusive/lambda', type: 'value'
expect(last_response.status).to eq 200
expect(last_response.body).to eq({ type: 'value' }.to_json)
end

it 'rejects values that matches except' do
get '/except/exclusive/lambda', type: 'invalid-type1'
expect(last_response.status).to eq 400
expect(last_response.body).to eq({ error: 'type has a value not allowed' }.to_json)
end
end

context 'exclusive excepts with lambda and coercion' do
it 'allows any other value outside excepts' do
get '/except/exclusive/lambda/coercion', type: '10010000'
expect(last_response.status).to eq 200
expect(last_response.body).to eq({ type: 10_010_000 }.to_json)
end

it 'rejects values that matches except' do
get '/except/exclusive/lambda/coercion', type: '3'
expect(last_response.status).to eq 400
expect(last_response.body).to eq({ error: 'type has a value not allowed' }.to_json)
end
end

context 'with mixed values and excepts' do
it 'allows value, but not in except' do
get '/mixed/value/except', type: 2
Expand Down