Skip to content

Commit

Permalink
wrote failing tests to prove condition of lambda and coercion working…
Browse files Browse the repository at this point in the history
… correctly
  • Loading branch information
jonmchan committed Sep 15, 2016
1 parent 220c345 commit c54e18c
Showing 1 changed file with 57 additions and 0 deletions.
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: '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/coercion', 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 'with mixed values and excepts' do
it 'allows value, but not in except' do
get '/mixed/value/except', type: 2
Expand Down

0 comments on commit c54e18c

Please sign in to comment.