@@ -227,16 +227,6 @@ def define_requires_none
227
227
expect ( JSON . parse ( last_response . body ) ) . to eq ( 'items' => [ ] )
228
228
end
229
229
230
- it "doesn't allow any key in the options hash other than type" do
231
- expect {
232
- subject . params do
233
- requires ( :items , desc : 'Foo' ) do
234
- requires :key
235
- end
236
- end
237
- } . to raise_error ArgumentError
238
- end
239
-
240
230
it 'adds to declared parameters' do
241
231
subject . params do
242
232
requires :items do
@@ -281,16 +271,6 @@ def define_requires_none
281
271
expect ( last_response . body ) . to eq ( 'required works' )
282
272
end
283
273
284
- it "doesn't allow any key in the options hash other than type" do
285
- expect {
286
- subject . params do
287
- requires ( :items , desc : 'Foo' ) do
288
- requires :key
289
- end
290
- end
291
- } . to raise_error ArgumentError
292
- end
293
-
294
274
it 'adds to declared parameters' do
295
275
subject . params do
296
276
requires :items do
@@ -335,6 +315,63 @@ def define_requires_none
335
315
end
336
316
end
337
317
318
+ context 'custom validator for a Hash' do
319
+ module DateRangeValidations
320
+ class DateRangeValidator < Grape ::Validations ::Base
321
+ def validate_param! ( attr_name , params )
322
+ unless params [ attr_name ] [ :from ] <= params [ attr_name ] [ :to ]
323
+ raise Grape ::Exceptions ::Validation , params : [ @scope . full_name ( attr_name ) ] , message : "'from' must be lower or equal to 'to'"
324
+ end
325
+ end
326
+ end
327
+ end
328
+
329
+ before do
330
+ subject . params do
331
+ optional :date_range , date_range : true , type : Hash do
332
+ requires :from , type : Integer
333
+ requires :to , type : Integer
334
+ end
335
+ end
336
+ subject . get ( '/optional' ) do
337
+ 'optional works'
338
+ end
339
+ subject . params do
340
+ requires :date_range , date_range : true , type : Hash do
341
+ requires :from , type : Integer
342
+ requires :to , type : Integer
343
+ end
344
+ end
345
+ subject . get ( '/required' ) do
346
+ 'required works'
347
+ end
348
+ end
349
+
350
+ context 'which is optional' do
351
+ it "doesn't throw an error if the validation passes" do
352
+ get '/optional' , date_range : { from : 1 , to : 2 }
353
+ expect ( last_response . status ) . to eq ( 200 )
354
+ end
355
+
356
+ it 'errors if the validation fails' do
357
+ get '/optional' , date_range : { from : 2 , to : 1 }
358
+ expect ( last_response . status ) . to eq ( 400 )
359
+ end
360
+ end
361
+
362
+ context 'which is required' do
363
+ it "doesn't throw an error if the validation passes" do
364
+ get '/required' , date_range : { from : 1 , to : 2 }
365
+ expect ( last_response . status ) . to eq ( 200 )
366
+ end
367
+
368
+ it 'errors if the validation fails' do
369
+ get '/required' , date_range : { from : 2 , to : 1 }
370
+ expect ( last_response . status ) . to eq ( 400 )
371
+ end
372
+ end
373
+ end
374
+
338
375
context 'validation within arrays' do
339
376
before do
340
377
subject . params do
0 commit comments