File tree 1 file changed +38
-0
lines changed
spec/grape/validations/validators
1 file changed +38
-0
lines changed Original file line number Diff line number Diff line change @@ -706,6 +706,44 @@ def self.parse(_val)
706
706
expect ( JSON . parse ( last_response . body ) ) . to eq ( [ 1 , 1 , 1 , 1 ] )
707
707
end
708
708
709
+ context 'Array type and coerce_with should' do
710
+ before do
711
+ subject . params do
712
+ optional :arr , type : Array , coerce_with : ( lambda do |val |
713
+ if val . nil?
714
+ [ ]
715
+ else
716
+ val
717
+ end
718
+ end )
719
+ end
720
+ subject . get '/' do
721
+ params [ :arr ] . class . to_s
722
+ end
723
+ end
724
+
725
+ it 'coerce nil value to array' do
726
+ get '/' , arr : nil
727
+
728
+ expect ( last_response . status ) . to eq ( 200 )
729
+ expect ( last_response . body ) . to eq ( 'Array' )
730
+ end
731
+
732
+ it 'not coerce missing field' do
733
+ get '/'
734
+
735
+ expect ( last_response . status ) . to eq ( 200 )
736
+ expect ( last_response . body ) . to eq ( 'NilClass' )
737
+ end
738
+
739
+ it 'coerce array as array' do
740
+ get '/' , arr : [ ]
741
+
742
+ expect ( last_response . status ) . to eq ( 200 )
743
+ expect ( last_response . body ) . to eq ( 'Array' )
744
+ end
745
+ end
746
+
709
747
it 'uses parse where available' do
710
748
subject . params do
711
749
requires :ints , type : Array , coerce_with : JSON do
You can’t perform that action at this time.
0 commit comments