@@ -181,7 +181,6 @@ def initialize(value)
181
181
182
182
subject . post ( '/required' ) { 'coercion with proc works' }
183
183
post '/required' , numbers : '10'
184
- p last_response . body
185
184
expect ( last_response . status ) . to eq ( 201 )
186
185
expect ( last_response . body ) . to eq ( 'coercion with proc works' )
187
186
end
@@ -372,6 +371,44 @@ def initialize(value)
372
371
expect ( last_response . status ) . to eq ( 200 )
373
372
end
374
373
374
+ it 'applies only the appropriate validation' do
375
+ subject . params do
376
+ optional :a
377
+ optional :b
378
+ mutually_exclusive :a , :b
379
+ given :a do
380
+ requires :c , type : String
381
+ end
382
+ given :b do
383
+ requires :c , type : Integer
384
+ end
385
+ end
386
+ subject . get ( '/multiple' ) { declared ( params ) . to_json }
387
+
388
+ get '/multiple'
389
+ expect ( last_response . status ) . to eq ( 200 )
390
+
391
+ get '/multiple' , a : true , c : 'test'
392
+ expect ( last_response . status ) . to eq ( 200 )
393
+ expect ( JSON . parse ( last_response . body ) . symbolize_keys ) . to eq a : 'true' , b : nil , c : 'test'
394
+
395
+ get '/multiple' , b : true , c : '3'
396
+ expect ( last_response . status ) . to eq ( 200 )
397
+ expect ( JSON . parse ( last_response . body ) . symbolize_keys ) . to eq a : nil , b : 'true' , c : 3
398
+
399
+ get '/multiple' , a : true
400
+ expect ( last_response . status ) . to eq ( 400 )
401
+ expect ( last_response . body ) . to eq ( 'c is missing' )
402
+
403
+ get '/multiple' , b : true
404
+ expect ( last_response . status ) . to eq ( 400 )
405
+ expect ( last_response . body ) . to eq ( 'c is missing' )
406
+
407
+ get '/multiple' , a : true , b : true , c : 'test'
408
+ expect ( last_response . status ) . to eq ( 400 )
409
+ expect ( last_response . body ) . to eq ( 'a, b are mutually exclusive, c is invalid' )
410
+ end
411
+
375
412
it 'raises an error if the dependent parameter was never specified' do
376
413
expect do
377
414
subject . params do
@@ -437,89 +474,114 @@ def initialize(value)
437
474
end
438
475
439
476
context 'when validations are dependent on a parameter with specific value' do
440
- before do
441
- subject . params do
442
- optional :a
443
- given a : -> ( val ) { val == 'x' } do
444
- requires :b
477
+ # build test cases from all combinations of declarations and options
478
+ a_decls = %i( optional requires )
479
+ a_options = [ { } , { values : %w( x y z ) } ]
480
+ b_options = [ { } , { type : String } , { allow_blank : false } , { type : String , allow_blank : false } ]
481
+ combinations = a_decls . product ( a_options , b_options )
482
+ combinations . each_with_index do |combination , i |
483
+ a_decl , a_opts , b_opts = combination
484
+
485
+ context "(case #{ i } )" do
486
+ before do
487
+ # puts "a_decl: #{a_decl}, a_opts: #{a_opts}, b_opts: #{b_opts}"
488
+ subject . params do
489
+ send a_decl , :a , **a_opts
490
+ given ( a : -> ( val ) { val == 'x' } ) { requires :b , **b_opts }
491
+ given ( a : -> ( val ) { val == 'y' } ) { requires :c , **b_opts }
492
+ end
493
+ subject . get ( '/test' ) { declared ( params ) . to_json }
445
494
end
446
- end
447
- subject . get ( '/test' ) { declared ( params ) . to_json }
448
- end
449
-
450
- it 'applies the validations only if the parameter has the specific value' do
451
- get '/test'
452
- expect ( last_response . status ) . to eq ( 200 )
453
495
454
- get '/test' , a : 'x'
455
- expect ( last_response . status ) . to eq ( 400 )
456
- expect ( last_response . body ) . to eq ( 'b is missing' )
496
+ if a_decl == :optional
497
+ it 'skips validation when base param is missing' do
498
+ get '/test'
499
+ expect ( last_response . status ) . to eq ( 200 )
500
+ end
501
+ end
457
502
458
- get '/test' , a : 'x' , b : true
459
- expect ( last_response . status ) . to eq ( 200 )
460
- end
503
+ it 'skips validation when base param does not have a specified value' do
504
+ get '/test' , a : 'z'
505
+ expect ( last_response . status ) . to eq ( 200 )
461
506
462
- it 'raises an error if the dependent parameter was never specified' do
463
- expect do
464
- subject . params do
465
- given :c do
466
- end
507
+ get '/test' , a : 'z' , b : ''
508
+ expect ( last_response . status ) . to eq ( 200 )
467
509
end
468
- end . to raise_error ( Grape ::Exceptions ::UnknownParameter )
469
- end
470
510
471
- it 'includes the parameter within #declared(params)' do
472
- get '/test' , a : true , b : true
511
+ it 'applies the validation when base param has the specific value' do
512
+ get '/test' , a : 'x'
513
+ expect ( last_response . status ) . to eq ( 400 )
514
+ expect ( last_response . body ) . to include ( 'b is missing' )
473
515
474
- expect ( JSON . parse ( last_response . body ) ) . to eq ( 'a' => 'true' , 'b' => 'true' )
516
+ get '/test' , a : 'x' , b : true
517
+ expect ( last_response . status ) . to eq ( 200 )
518
+
519
+ get '/test' , a : 'x' , b : true , c : ''
520
+ expect ( last_response . status ) . to eq ( 200 )
521
+ end
522
+
523
+ it 'includes the parameter within #declared(params)' do
524
+ get '/test' , a : 'x' , b : true
525
+ expect ( JSON . parse ( last_response . body ) ) . to eq ( 'a' => 'x' , 'b' => 'true' , 'c' => nil )
526
+ end
527
+ end
475
528
end
529
+ end
476
530
477
- it 'returns a sensible error message within a nested context' do
531
+ it 'raises an error if the dependent parameter was never specified' do
532
+ expect do
478
533
subject . params do
479
- requires :bar , type : Hash do
480
- optional :a
481
- given a : -> ( val ) { val == 'x' } do
482
- requires :b
483
- end
534
+ given :c do
484
535
end
485
536
end
486
- subject . get ( '/nested' ) { 'worked' }
537
+ end . to raise_error ( Grape ::Exceptions ::UnknownParameter )
538
+ end
487
539
488
- get '/nested' , bar : { a : 'x' }
489
- expect ( last_response . status ) . to eq ( 400 )
490
- expect ( last_response . body ) . to eq ( 'bar[b] is missing' )
540
+ it 'returns a sensible error message within a nested context' do
541
+ subject . params do
542
+ requires :bar , type : Hash do
543
+ optional :a
544
+ given a : -> ( val ) { val == 'x' } do
545
+ requires :b
546
+ end
547
+ end
491
548
end
549
+ subject . get ( '/nested' ) { 'worked' }
492
550
493
- it 'includes the nested parameter within #declared(params)' do
494
- subject . params do
495
- requires :bar , type : Hash do
496
- optional :a
497
- given a : -> ( val ) { val == 'x' } do
498
- requires :b
499
- end
551
+ get '/nested' , bar : { a : 'x' }
552
+ expect ( last_response . status ) . to eq ( 400 )
553
+ expect ( last_response . body ) . to eq ( 'bar[b] is missing' )
554
+ end
555
+
556
+ it 'includes the nested parameter within #declared(params)' do
557
+ subject . params do
558
+ requires :bar , type : Hash do
559
+ optional :a
560
+ given a : -> ( val ) { val == 'x' } do
561
+ requires :b
500
562
end
501
563
end
502
- subject . get ( '/nested' ) { declared ( params ) . to_json }
503
-
504
- get '/nested' , bar : { a : 'x' , b : 'yes' }
505
- expect ( JSON . parse ( last_response . body ) ) . to eq ( 'bar' => { 'a' => 'x' , 'b' => 'yes' } )
506
564
end
565
+ subject . get ( '/nested' ) { declared ( params ) . to_json }
507
566
508
- it 'includes level 2 nested parameters outside the given within #declared(params)' do
509
- subject . params do
510
- requires :bar , type : Hash do
511
- optional :a
512
- given a : -> ( val ) { val == 'x' } do
513
- requires :c , type : Hash do
514
- requires :b
515
- end
567
+ get '/nested' , bar : { a : 'x' , b : 'yes' }
568
+ expect ( JSON . parse ( last_response . body ) ) . to eq ( 'bar' => { 'a' => 'x' , 'b' => 'yes' } )
569
+ end
570
+
571
+ it 'includes level 2 nested parameters outside the given within #declared(params)' do
572
+ subject . params do
573
+ requires :bar , type : Hash do
574
+ optional :a
575
+ given a : -> ( val ) { val == 'x' } do
576
+ requires :c , type : Hash do
577
+ requires :b
516
578
end
517
579
end
518
580
end
519
- subject . get ( '/nested' ) { declared ( params ) . to_json }
520
-
521
- get '/nested' , bar : { a : 'x' , c : { b : 'yes' } }
522
- expect ( JSON . parse ( last_response . body ) ) . to eq ( 'bar' => { 'a' => 'x' , 'c' => { 'b' => 'yes' } } )
523
581
end
582
+ subject . get ( '/nested' ) { declared ( params ) . to_json }
583
+
584
+ get '/nested' , bar : { a : 'x' , c : { b : 'yes' } }
585
+ expect ( JSON . parse ( last_response . body ) ) . to eq ( 'bar' => { 'a' => 'x' , 'c' => { 'b' => 'yes' } } )
524
586
end
525
587
end
0 commit comments