/
params_scope_spec.rb
1183 lines (1007 loc) · 33.8 KB
/
params_scope_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# frozen_string_literal: true
require 'spec_helper'
describe Grape::Validations::ParamsScope do
subject do
Class.new(Grape::API)
end
def app
subject
end
context 'setting a default' do
let(:documentation) { subject.routes.first.params }
context 'when the default value is truthy' do
before do
subject.params do
optional :int, type: Integer, default: 42
end
subject.get
end
it 'adds documentation about the default value' do
expect(documentation).to have_key('int')
expect(documentation['int']).to have_key(:default)
expect(documentation['int'][:default]).to eq(42)
end
end
context 'when the default value is false' do
before do
subject.params do
optional :bool, type: Grape::API::Boolean, default: false
end
subject.get
end
it 'adds documentation about the default value' do
expect(documentation).to have_key('bool')
expect(documentation['bool']).to have_key(:default)
expect(documentation['bool'][:default]).to eq(false)
end
end
context 'when the default value is nil' do
before do
subject.params do
optional :object, type: Object, default: nil
end
subject.get
end
it 'adds documentation about the default value' do
expect(documentation).to have_key('object')
expect(documentation['object']).to have_key(:default)
expect(documentation['object'][:default]).to eq(nil)
end
end
end
context 'without a default' do
before do
subject.params do
optional :object, type: Object
end
subject.get
end
it 'does not add documentation for the default value' do
documentation = subject.routes.first.params
expect(documentation).to have_key('object')
expect(documentation['object']).not_to have_key(:default)
end
end
context 'setting description' do
%i[desc description].each do |description_type|
it "allows setting #{description_type}" do
subject.params do
requires :int, type: Integer, description_type => 'My very nice integer'
end
subject.get '/single' do
'int works'
end
get '/single', int: 420
expect(last_response.status).to eq(200)
expect(last_response.body).to eq('int works')
end
end
end
context 'when using custom types' do
module ParamsScopeSpec
class CustomType
attr_reader :value
def self.parse(value)
raise if value == 'invalid'
new(value)
end
def initialize(value)
@value = value
end
end
end
it 'coerces the parameter via the type\'s parse method' do
subject.params do
requires :foo, type: ParamsScopeSpec::CustomType
end
subject.get('/types') { params[:foo].value }
get '/types', foo: 'valid'
expect(last_response.status).to eq(200)
expect(last_response.body).to eq('valid')
get '/types', foo: 'invalid'
expect(last_response.status).to eq(400)
expect(last_response.body).to match(/foo is invalid/)
end
end
context 'param renaming' do
it do
subject.params do
requires :foo, as: :bar
optional :super, as: :hiper
end
subject.get('/renaming') { "#{declared(params)['bar']}-#{declared(params)['hiper']}" }
get '/renaming', foo: 'any', super: 'any2'
expect(last_response.status).to eq(200)
expect(last_response.body).to eq('any-any2')
end
it do
subject.params do
requires :foo, as: :bar, type: String, coerce_with: ->(c) { c.strip }
end
subject.get('/renaming-coerced') { "#{params['bar']}-#{params['foo']}" }
get '/renaming-coerced', foo: ' there we go '
expect(last_response.status).to eq(200)
expect(last_response.body).to eq('there we go-')
end
it do
subject.params do
requires :foo, as: :bar, allow_blank: false
end
subject.get('/renaming-not-blank') {}
get '/renaming-not-blank', foo: ''
expect(last_response.status).to eq(400)
expect(last_response.body).to eq('foo is empty')
end
it do
subject.params do
requires :foo, as: :bar, allow_blank: false
end
subject.get('/renaming-not-blank-with-value') {}
get '/renaming-not-blank-with-value', foo: 'any'
expect(last_response.status).to eq(200)
end
it do
subject.params do
requires :foo, as: :baz, type: Hash do
requires :bar, as: :qux
end
end
subject.get('/nested-renaming') { declared(params).to_json }
get '/nested-renaming', foo: { bar: 'any' }
expect(last_response.status).to eq(200)
expect(last_response.body).to eq('{"baz":{"qux":"any"}}')
end
end
context 'array without coerce type explicitly given' do
it 'sets the type based on first element' do
subject.params do
requires :periods, type: Array, values: -> { %w[day month] }
end
subject.get('/required') { 'required works' }
get '/required', periods: %w[day month]
expect(last_response.status).to eq(200)
expect(last_response.body).to eq('required works')
end
it 'fails to call API without Array type' do
subject.params do
requires :periods, type: Array, values: -> { %w[day month] }
end
subject.get('/required') { 'required works' }
get '/required', periods: 'day'
expect(last_response.status).to eq(400)
expect(last_response.body).to eq('periods is invalid')
end
it 'raises exception when values are of different type' do
expect do
subject.params { requires :numbers, type: Array, values: [1, 'definitely not a number', 3] }
end.to raise_error Grape::Exceptions::IncompatibleOptionValues
end
it 'raises exception when range values have different endpoint types' do
expect do
subject.params { requires :numbers, type: Array, values: 0.0..10 }
end.to raise_error Grape::Exceptions::IncompatibleOptionValues
end
end
context 'coercing values validation with proc' do
it 'allows the proc to pass validation without checking' do
subject.params { requires :numbers, type: Integer, values: -> { [0, 1, 2] } }
subject.post('/required') { 'coercion with proc works' }
post '/required', numbers: '1'
expect(last_response.status).to eq(201)
expect(last_response.body).to eq('coercion with proc works')
end
it 'allows the proc to pass validation without checking in value' do
subject.params { requires :numbers, type: Integer, values: { value: -> { [0, 1, 2] } } }
subject.post('/required') { 'coercion with proc works' }
post '/required', numbers: '1'
expect(last_response.status).to eq(201)
expect(last_response.body).to eq('coercion with proc works')
end
it 'allows the proc to pass validation without checking in except' do
subject.params { requires :numbers, type: Integer, values: { except: -> { [0, 1, 2] } } }
subject.post('/required') { 'coercion with proc works' }
post '/required', numbers: '10'
expect(last_response.status).to eq(201)
expect(last_response.body).to eq('coercion with proc works')
end
end
context 'with range values' do
context "when left range endpoint isn't #kind_of? the type" do
it 'raises exception' do
expect do
subject.params { requires :latitude, type: Integer, values: -90.0..90 }
end.to raise_error Grape::Exceptions::IncompatibleOptionValues
end
end
context "when right range endpoint isn't #kind_of? the type" do
it 'raises exception' do
expect do
subject.params { requires :latitude, type: Integer, values: -90..90.0 }
end.to raise_error Grape::Exceptions::IncompatibleOptionValues
end
end
context 'when the default is an array' do
context 'and is the entire range of allowed values' do
it 'does not raise an exception' do
expect do
subject.params { optional :numbers, type: Array[Integer], values: 0..2, default: 0..2 }
end.to_not raise_error
end
end
context 'and is a subset of allowed values' do
it 'does not raise an exception' do
expect do
subject.params { optional :numbers, type: Array[Integer], values: [0, 1, 2], default: [1, 0] }
end.to_not raise_error
end
end
end
context 'when both range endpoints are #kind_of? the type' do
it 'accepts values in the range' do
subject.params do
requires :letter, type: String, values: 'a'..'z'
end
subject.get('/letter') { params[:letter] }
get '/letter', letter: 'j'
expect(last_response.status).to eq(200)
expect(last_response.body).to eq('j')
end
it 'rejects values outside the range' do
subject.params do
requires :letter, type: String, values: 'a'..'z'
end
subject.get('/letter') { params[:letter] }
get '/letter', letter: 'J'
expect(last_response.status).to eq(400)
expect(last_response.body).to eq('letter does not have a valid value')
end
end
end
context 'parameters in group' do
it 'errors when no type is provided' do
expect do
subject.params do
group :a do
requires :b
end
end
end.to raise_error Grape::Exceptions::MissingGroupTypeError
expect do
subject.params do
optional :a do
requires :b
end
end
end.to raise_error Grape::Exceptions::MissingGroupTypeError
end
it 'allows Hash as type' do
subject.params do
group :a, type: Hash do
requires :b
end
end
subject.get('/group') { 'group works' }
get '/group', a: { b: true }
expect(last_response.status).to eq(200)
expect(last_response.body).to eq('group works')
subject.params do
optional :a, type: Hash do
requires :b
end
end
get '/optional_type_hash'
end
it 'allows Array as type' do
subject.params do
group :a, type: Array do
requires :b
end
end
subject.get('/group') { 'group works' }
get '/group', a: [{ b: true }]
expect(last_response.status).to eq(200)
expect(last_response.body).to eq('group works')
subject.params do
optional :a, type: Array do
requires :b
end
end
get '/optional_type_array'
end
it 'handles missing optional Array type' do
subject.params do
optional :a, type: Array do
requires :b
end
end
subject.get('/test') { declared(params).to_json }
get '/test'
expect(last_response.status).to eq(200)
expect(last_response.body).to eq('{"a":[]}')
end
it 'errors with an unsupported type' do
expect do
subject.params do
group :a, type: Set do
requires :b
end
end
end.to raise_error Grape::Exceptions::UnsupportedGroupTypeError
expect do
subject.params do
optional :a, type: Set do
requires :b
end
end
end.to raise_error Grape::Exceptions::UnsupportedGroupTypeError
end
end
context 'nested structures within hashes' do
it 'allows nesting of optional arrays within hashes' do
subject.params do
optional :data, type: Hash do
optional :array, type: Array do
optional :name, type: String
end
end
end
subject.post('/nesty') { declared(params).to_json }
post '/nesty', { data: {} }
expect(last_response.status).to eq 201
# {"data"=>{"array"=>{"name"=>nil}}}
expect(JSON.parse(last_response.body)['data']['array']).not_to be_a Hash
end
it 'allows nesting of optional arrays within hashes, JSON-style' do
subject.format :json
subject.params do
optional :data, type: JSON do
optional :array, type: Array[JSON] do
optional :name, type: String
end
end
end
subject.post('/nesty') { declared(params) }
body = { data: { } }.to_json
post '/nesty', body, "CONTENT_TYPE" => "application/json"
expect(last_response.status).to eq 201
# {"data"=>{"array"=>{"name"=>nil}}}
expect(JSON.parse(last_response.body)['data']['array']).not_to be_a Hash
end
it "doesn't try to add deeply nested optional hashes" do
# this may be working as intended?
subject.params do
optional :data, type: JSON do
optional :json, type: JSON do
optional :name, type: String
end
end
end
subject.post('/nesty') { declared(params).to_json }
post '/nesty', { data: { } }
expect(last_response.status).to eq 201
# {"data"=>{"json"=>{"name"=>nil}}}
expect(JSON.parse(last_response.body)['data']['json']).not_to be_a Hash
end
end
context 'when validations are dependent on a parameter' do
before do
subject.params do
optional :a
given :a do
requires :b
end
end
subject.get('/test') { declared(params).to_json }
end
it 'applies the validations only if the parameter is present' do
get '/test'
expect(last_response.status).to eq(200)
get '/test', a: true
expect(last_response.status).to eq(400)
expect(last_response.body).to eq('b is missing')
get '/test', a: true, b: true
expect(last_response.status).to eq(200)
end
it 'applies the validations of multiple parameters' do
subject.params do
optional :a, :b
given :a, :b do
requires :c
end
end
subject.get('/multiple') { declared(params).to_json }
get '/multiple'
expect(last_response.status).to eq(200)
get '/multiple', a: true
expect(last_response.status).to eq(200)
get '/multiple', b: true
expect(last_response.status).to eq(200)
get '/multiple', a: true, b: true
expect(last_response.status).to eq(400)
expect(last_response.body).to eq('c is missing')
get '/multiple', a: true, b: true, c: true
expect(last_response.status).to eq(200)
end
it 'applies only the appropriate validation' do
subject.params do
optional :a
optional :b
mutually_exclusive :a, :b
given :a do
requires :c, type: String
end
given :b do
requires :c, type: Integer
end
end
subject.get('/multiple') { declared(params).to_json }
get '/multiple'
expect(last_response.status).to eq(200)
get '/multiple', a: true, c: 'test'
expect(last_response.status).to eq(200)
expect(JSON.parse(last_response.body).symbolize_keys).to eq a: 'true', b: nil, c: 'test'
get '/multiple', b: true, c: '3'
expect(last_response.status).to eq(200)
expect(JSON.parse(last_response.body).symbolize_keys).to eq a: nil, b: 'true', c: 3
get '/multiple', a: true
expect(last_response.status).to eq(400)
expect(last_response.body).to eq('c is missing')
get '/multiple', b: true
expect(last_response.status).to eq(400)
expect(last_response.body).to eq('c is missing')
get '/multiple', a: true, b: true, c: 'test'
expect(last_response.status).to eq(400)
expect(last_response.body).to eq('a, b are mutually exclusive, c is invalid')
end
it 'raises an error if the dependent parameter was never specified' do
expect do
subject.params do
given :c do
end
end
end.to raise_error(Grape::Exceptions::UnknownParameter)
end
it 'does not raise an error if the dependent parameter is a Hash' do
expect do
subject.params do
optional :a, type: Hash do
requires :b
end
given :a do
requires :c
end
end
end.to_not raise_error
end
it 'does not raise an error if when using nested given' do
expect do
subject.params do
optional :a, type: Hash do
requires :b
end
given :a do
requires :c
given :c do
requires :d
end
end
end
end.to_not raise_error
end
it 'allows nested dependent parameters' do
subject.params do
optional :a
given a: ->(val) { val == 'a' } do
optional :b
given b: ->(val) { val == 'b' } do
optional :c
given c: ->(val) { val == 'c' } do
requires :d
end
end
end
end
subject.get('/') { declared(params).to_json }
get '/'
expect(last_response.status).to eq 200
get '/', a: 'a', b: 'b', c: 'c'
expect(last_response.status).to eq 400
expect(last_response.body).to eq 'd is missing'
get '/', a: 'a', b: 'b', c: 'c', d: 'd'
expect(last_response.status).to eq 200
expect(last_response.body).to eq({ a: 'a', b: 'b', c: 'c', d: 'd' }.to_json)
end
it 'allows renaming of dependent parameters' do
subject.params do
optional :a
given :a do
requires :b, as: :c
end
end
subject.get('/multiple') { declared(params).to_json }
get '/multiple', a: 'a', b: 'b'
body = JSON.parse(last_response.body)
expect(body.keys).to include('c')
expect(body.keys).to_not include('b')
end
it 'allows renaming of dependent on parameter' do
subject.params do
optional :a, as: :b
given b: ->(val) { val == 'x' } do
requires :c
end
end
subject.get('/') { declared(params) }
get '/', a: 'x'
expect(last_response.status).to eq 400
expect(last_response.body).to eq 'c is missing'
get '/', a: 'y'
expect(last_response.status).to eq 200
end
it 'raises an error if the dependent parameter is not the renamed one' do
expect do
subject.params do
optional :a, as: :b
given :a do
requires :c
end
end
end.to raise_error(Grape::Exceptions::UnknownParameter)
end
it 'does not validate nested requires when given is false' do
subject.params do
requires :a, type: String, allow_blank: false, values: %w[x y z]
given a: ->(val) { val == 'x' } do
requires :inner1, type: Hash, allow_blank: false do
requires :foo, type: Integer, allow_blank: false
end
end
given a: ->(val) { val == 'y' } do
requires :inner2, type: Hash, allow_blank: false do
requires :bar, type: Integer, allow_blank: false
requires :baz, type: Array, allow_blank: false do
requires :baz_category, type: String, allow_blank: false
end
end
end
given a: ->(val) { val == 'z' } do
requires :inner3, type: Array, allow_blank: false do
requires :bar, type: Integer, allow_blank: false
requires :baz, type: Array, allow_blank: false do
requires :baz_category, type: String, allow_blank: false
end
end
end
end
subject.get('/varying') { declared(params).to_json }
get '/varying', a: 'x', inner1: { foo: 1 }
expect(last_response.status).to eq(200)
get '/varying', a: 'y', inner2: { bar: 2, baz: [{ baz_category: 'barstools' }] }
expect(last_response.status).to eq(200)
get '/varying', a: 'y', inner2: { bar: 2, baz: [{ unrelated: 'yep' }] }
expect(last_response.status).to eq(400)
get '/varying', a: 'z', inner3: [{ bar: 3, baz: [{ baz_category: 'barstools' }] }]
expect(last_response.status).to eq(200)
end
it 'includes the parameter within #declared(params)' do
get '/test', a: true, b: true
expect(JSON.parse(last_response.body)).to eq('a' => 'true', 'b' => 'true')
end
it 'returns a sensible error message within a nested context' do
subject.params do
requires :bar, type: Hash do
optional :a
given :a do
requires :b
end
end
end
subject.get('/nested') { 'worked' }
get '/nested', bar: { a: true }
expect(last_response.status).to eq(400)
expect(last_response.body).to eq('bar[b] is missing')
end
it 'includes the nested parameter within #declared(params)' do
subject.params do
requires :bar, type: Hash do
optional :a
given :a do
requires :b
end
end
end
subject.get('/nested') { declared(params).to_json }
get '/nested', bar: { a: true, b: 'yes' }
expect(JSON.parse(last_response.body)).to eq('bar' => { 'a' => 'true', 'b' => 'yes' })
end
it 'includes level 2 nested parameters outside the given within #declared(params)' do
subject.params do
requires :bar, type: Hash do
optional :a
given :a do
requires :c, type: Hash do
requires :b
end
end
end
end
subject.get('/nested') { declared(params).to_json }
get '/nested', bar: { a: true, c: { b: 'yes' } }
expect(JSON.parse(last_response.body)).to eq('bar' => { 'a' => 'true', 'c' => { 'b' => 'yes' } })
end
end
context 'default value in given block' do
before do
subject.params do
optional :a, values: %w[a b]
given a: ->(val) { val == 'a' } do
optional :b, default: 'default'
end
end
subject.get('/') { params.to_json }
end
context 'when dependency meets' do
it 'sets default value for dependent parameter' do
get '/', a: 'a'
expect(last_response.body).to eq({ a: 'a', b: 'default' }.to_json)
end
end
context 'when dependency does not meet' do
it 'does not set default value for dependent parameter' do
get '/', a: 'b'
expect(last_response.body).to eq({ a: 'b' }.to_json)
end
end
end
context 'when validations are dependent on a parameter within an array param' do
before do
subject.params do
requires :foos, type: Array do
optional :foo
given :foo do
requires :bar
end
end
end
subject.get('/test') { 'ok' }
end
it 'should pass none Hash params' do
get '/test', foos: ['']
expect(last_response.status).to eq(200)
expect(last_response.body).to eq('ok')
end
end
context 'when validations are dependent on a parameter within an array param within #declared(params).to_json' do
before do
subject.params do
requires :foos, type: Array do
optional :foo_type, :baz_type
given :foo_type do
requires :bar
end
end
end
subject.post('/test') { declared(params).to_json }
end
it 'applies the constraint within each value' do
post '/test',
{ foos: [{ foo_type: 'a' }, { baz_type: 'c' }] }.to_json,
'CONTENT_TYPE' => 'application/json'
expect(last_response.status).to eq(400)
expect(last_response.body).to eq('foos[0][bar] is missing')
end
end
context 'when validations are dependent on a parameter with specific value' do
# build test cases from all combinations of declarations and options
a_decls = %i[optional requires]
a_options = [{}, { values: %w[x y z] }]
b_options = [{}, { type: String }, { allow_blank: false }, { type: String, allow_blank: false }]
combinations = a_decls.product(a_options, b_options)
combinations.each_with_index do |combination, i|
a_decl, a_opts, b_opts = combination
context "(case #{i})" do
before do
# puts "a_decl: #{a_decl}, a_opts: #{a_opts}, b_opts: #{b_opts}"
subject.params do
send a_decl, :a, **a_opts
given(a: ->(val) { val == 'x' }) { requires :b, **b_opts }
given(a: ->(val) { val == 'y' }) { requires :c, **b_opts }
end
subject.get('/test') { declared(params).to_json }
end
if a_decl == :optional
it 'skips validation when base param is missing' do
get '/test'
expect(last_response.status).to eq(200)
end
end
it 'skips validation when base param does not have a specified value' do
get '/test', a: 'z'
expect(last_response.status).to eq(200)
get '/test', a: 'z', b: ''
expect(last_response.status).to eq(200)
end
it 'applies the validation when base param has the specific value' do
get '/test', a: 'x'
expect(last_response.status).to eq(400)
expect(last_response.body).to include('b is missing')
get '/test', a: 'x', b: true
expect(last_response.status).to eq(200)
get '/test', a: 'x', b: true, c: ''
expect(last_response.status).to eq(200)
end
it 'includes the parameter within #declared(params)' do
get '/test', a: 'x', b: true
expect(JSON.parse(last_response.body)).to eq('a' => 'x', 'b' => 'true', 'c' => nil)
end
end
end
end
it 'raises an error if the dependent parameter was never specified' do
expect do
subject.params do
given :c do
end
end
end.to raise_error(Grape::Exceptions::UnknownParameter)
end
it 'returns a sensible error message within a nested context' do
subject.params do
requires :bar, type: Hash do
optional :a
given a: ->(val) { val == 'x' } do
requires :b
end
end
end
subject.get('/nested') { 'worked' }
get '/nested', bar: { a: 'x' }
expect(last_response.status).to eq(400)
expect(last_response.body).to eq('bar[b] is missing')
end
it 'includes the nested parameter within #declared(params)' do
subject.params do
requires :bar, type: Hash do
optional :a
given a: ->(val) { val == 'x' } do
requires :b
end
end
end
subject.get('/nested') { declared(params).to_json }
get '/nested', bar: { a: 'x', b: 'yes' }
expect(JSON.parse(last_response.body)).to eq('bar' => { 'a' => 'x', 'b' => 'yes' })
end
it 'includes level 2 nested parameters outside the given within #declared(params)' do
subject.params do
requires :bar, type: Hash do
optional :a
given a: ->(val) { val == 'x' } do
requires :c, type: Hash do
requires :b
end
end
end
end
subject.get('/nested') { declared(params).to_json }
get '/nested', bar: { a: 'x', c: { b: 'yes' } }
expect(JSON.parse(last_response.body)).to eq('bar' => { 'a' => 'x', 'c' => { 'b' => 'yes' } })
end
it 'includes deeply nested parameters within #declared(params)' do
subject.params do
requires :arr1, type: Array do
requires :hash1, type: Hash do
requires :arr2, type: Array do
requires :hash2, type: Hash do
requires :something, type: String
end
end
end
end
end
subject.get('/nested_deep') { declared(params).to_json }
get '/nested_deep', arr1: [{ hash1: { arr2: [{ hash2: { something: 'value' } }] } }]
expect(last_response.status).to eq(200)
expect(JSON.parse(last_response.body)).to eq('arr1' => [{ 'hash1' => { 'arr2' => [{ 'hash2' => { 'something' => 'value' } }] } }])
end
context 'failing fast' do
context 'when fail_fast is not defined' do
it 'does not stop validation' do
subject.params do
requires :one
requires :two
requires :three
end
subject.get('/fail-fast') { declared(params).to_json }
get '/fail-fast'
expect(last_response.status).to eq(400)
expect(last_response.body).to eq('one is missing, two is missing, three is missing')
end
end
context 'when fail_fast is defined it stops the validation' do
it 'of other params' do
subject.params do
requires :one, fail_fast: true
requires :two
end
subject.get('/fail-fast') { declared(params).to_json }
get '/fail-fast'
expect(last_response.status).to eq(400)
expect(last_response.body).to eq('one is missing')
end
it 'for a single param' do
subject.params do
requires :one, allow_blank: false, regexp: /[0-9]+/, fail_fast: true
end
subject.get('/fail-fast') { declared(params).to_json }
get '/fail-fast', one: ''
expect(last_response.status).to eq(400)
expect(last_response.body).to eq('one is empty')
end
end
end
context 'when params have group attributes' do
context 'with validations' do
before do
subject.params do
with(allow_blank: false) do
requires :id
optional :name
optional :address, allow_blank: true
end
end
subject.get('test')
end
context 'when data is invalid' do
before do
get 'test', id: '', name: ''