@@ -54,6 +54,13 @@ class API < Grape::API
54
54
{ type : params [ :type ] }
55
55
end
56
56
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
+
57
64
params do
58
65
requires :type , values : { except : ValuesModel . excepts , message : 'default exclude message' }
59
66
end
@@ -132,6 +139,20 @@ class API < Grape::API
132
139
{ type : params [ :type ] }
133
140
end
134
141
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
+
135
156
params do
136
157
requires :type , type : Integer , values : { value : 1 ..5 , except : [ 3 ] }
137
158
end
@@ -182,6 +203,14 @@ def app
182
203
end
183
204
end
184
205
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
+
185
214
context 'exclude with a standard custom validation message' do
186
215
it 'does not allow an invalid value for a parameter' do
187
216
get ( '/custom_message/exclude/fallback_message' , type : 'invalid-type1' )
@@ -391,6 +420,34 @@ def app
391
420
end
392
421
end
393
422
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
+
394
451
context 'with mixed values and excepts' do
395
452
it 'allows value, but not in except' do
396
453
get '/mixed/value/except' , type : 2
0 commit comments