Skip to content

Commit a5af36b

Browse files
committed
wrote failing tests to prove condition of lambda and coercion working correctly
1 parent 220c345 commit a5af36b

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

spec/grape/validations/validators/values_spec.rb

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,13 @@ class API < Grape::API
5454
{ type: params[:type] }
5555
end
5656

57+
params do
58+
requires :type, values: { except: -> { ValuesModel.excepts }, except_message: 'value is on exclusions list' }
59+
end
60+
get '/exclude/lambda/exclude_message' do
61+
{ type: params[:type] }
62+
end
63+
5764
params do
5865
requires :type, values: { except: ValuesModel.excepts, message: 'default exclude message' }
5966
end
@@ -132,6 +139,20 @@ class API < Grape::API
132139
{ type: params[:type] }
133140
end
134141

142+
params do
143+
requires :type, values: { except: -> { ValuesModel.excepts } }
144+
end
145+
get '/except/exclusive/lambda' do
146+
{ type: params[:type] }
147+
end
148+
149+
params do
150+
requires :type, type: Integer, values: { except: -> { [3, 4, 5] } }
151+
end
152+
get '/except/exclusive/lambda/coercion' do
153+
{ type: params[:type] }
154+
end
155+
135156
params do
136157
requires :type, type: Integer, values: { value: 1..5, except: [3] }
137158
end
@@ -182,6 +203,14 @@ def app
182203
end
183204
end
184205

206+
context 'with a custom exclude validation message' do
207+
it 'does not allow an invalid value for a parameter' do
208+
get('/custom_message/exclude/lambda/exclude_message', type: 'invalid-type1')
209+
expect(last_response.status).to eq 400
210+
expect(last_response.body).to eq({ error: 'type value is on exclusions list' }.to_json)
211+
end
212+
end
213+
185214
context 'exclude with a standard custom validation message' do
186215
it 'does not allow an invalid value for a parameter' do
187216
get('/custom_message/exclude/fallback_message', type: 'invalid-type1')
@@ -391,6 +420,34 @@ def app
391420
end
392421
end
393422

423+
context 'exclusive excepts with lambda' do
424+
it 'allows any other value outside excepts' do
425+
get '/except/exclusive/lambda', type: 'value'
426+
expect(last_response.status).to eq 200
427+
expect(last_response.body).to eq({ type: 'value' }.to_json)
428+
end
429+
430+
it 'rejects values that matches except' do
431+
get '/except/exclusive/lambda', type: 'invalid-type1'
432+
expect(last_response.status).to eq 400
433+
expect(last_response.body).to eq({ error: 'type has a value not allowed' }.to_json)
434+
end
435+
end
436+
437+
context 'exclusive excepts with lambda and coercion' do
438+
it 'allows any other value outside excepts' do
439+
get '/except/exclusive/lambda/coercion', type: '10010000'
440+
expect(last_response.status).to eq 200
441+
expect(last_response.body).to eq({ type: 'value' }.to_json)
442+
end
443+
444+
it 'rejects values that matches except' do
445+
get '/except/exclusive/lambda/coercion', type: '3'
446+
expect(last_response.status).to eq 400
447+
expect(last_response.body).to eq({ error: 'type has a value not allowed' }.to_json)
448+
end
449+
end
450+
394451
context 'with mixed values and excepts' do
395452
it 'allows value, but not in except' do
396453
get '/mixed/value/except', type: 2

0 commit comments

Comments
 (0)