Skip to content

Commit 483a7a8

Browse files
committed
Merge pull request ruby-grape#207 from tim-vandecasteele/adapt_validation
Add support for Rack::Multipart::UploadedFile and don't care about parameter presence
2 parents 8d98e1d + b721815 commit 483a7a8

File tree

2 files changed

+37
-14
lines changed

2 files changed

+37
-14
lines changed

lib/grape/validations/coerce.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ def validate_param!(attr_name, params)
1616
end
1717
end
1818

19+
class InvalidValue; end
1920
private
2021

2122
def _valid_array_type?(type, values)
@@ -25,8 +26,12 @@ def _valid_array_type?(type, values)
2526
end
2627

2728
def _valid_single_type?(klass, val)
29+
# allow nil, to ignore when a parameter is absent
30+
return true if val.nil?
2831
if klass == Virtus::Attribute::Boolean
2932
val.is_a?(TrueClass) || val.is_a?(FalseClass)
33+
elsif klass == Rack::Multipart::UploadedFile
34+
val.is_a?(Hashie::Mash) && val.key?(:tempfile)
3035
else
3136
val.is_a?(klass)
3237
end
@@ -47,7 +52,7 @@ def coerce_value(type, val)
4752
# not the prettiest but some invalid coercion can currently trigger
4853
# errors in Virtus (see coerce_spec.rb)
4954
rescue
50-
nil
55+
InvalidValue.new
5156
end
5257

5358
end

spec/grape/validations/coerce_spec.rb

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class User
88
attribute :id, Integer
99
attribute :name, String
1010
end
11-
11+
1212
class API < Grape::API
1313
default_format :json
1414

@@ -32,6 +32,7 @@ class API < Grape::API
3232

3333
params do
3434
requires :int, :coerce => Integer
35+
optional :int2, :coerce => Integer
3536
optional :arr, :coerce => Array[Integer]
3637
optional :bool, :coerce => Array[Boolean]
3738
end
@@ -42,52 +43,69 @@ class API < Grape::API
4243
:bool => params[:bool] ? (params[:bool][0] == true) && (params[:bool][1] == false) : nil
4344
}
4445
end
46+
params do
47+
requires :uploaded_file, :type => Rack::Multipart::UploadedFile
48+
end
49+
post '/file' do
50+
{
51+
:dpx_file => params[:uploaded_file]
52+
}
53+
end
4554
end
4655
end
4756
end
48-
57+
4958
def app
5059
ValidationsSpec::CoerceValidatorSpec::API
5160
end
52-
61+
5362
it "should return an error on malformed input" do
5463
get '/single', :int => "43a"
5564
last_response.status.should == 400
56-
65+
5766
get '/single', :int => "43"
5867
last_response.status.should == 200
5968
end
60-
69+
6170
it "should return an error on malformed input (array)" do
6271
get '/arr', :ids => ["1", "2", "az"]
6372
last_response.status.should == 400
64-
73+
6574
get '/arr', :ids => ["1", "2", "890"]
6675
last_response.status.should == 200
6776
end
68-
77+
6978
it "should return an error on malformed input (complex object)" do
7079
# this request does raise an error inside Virtus
7180
get '/user', :user => "32"
7281
last_response.status.should == 400
73-
82+
7483
get '/user', :user => { :id => 32, :name => "Bob"}
7584
last_response.status.should == 200
7685
end
77-
86+
7887
it 'should coerce inputs' do
79-
get('/coerce', :int => "43")
88+
get('/coerce', :int => "43", :int2 => "42")
8089
last_response.status.should == 200
8190
ret = MultiJson.load(last_response.body)
8291
ret["int"].should == "Fixnum"
83-
84-
get('/coerce', :int => "40", :arr => ["1","20","3"], :bool => [1, 0])
92+
93+
get('/coerce', :int => "40", :int2 => "42", :arr => ["1","20","3"], :bool => [1, 0])
8594
# last_response.body.should == ""
8695
last_response.status.should == 200
8796
ret = MultiJson.load(last_response.body)
8897
ret["int"].should == "Fixnum"
8998
ret["arr"].should == "Fixnum"
9099
ret["bool"].should == true
91100
end
92-
101+
102+
it 'should not return an error when an optional parameter is nil' do
103+
get('/coerce', :int => "40")
104+
last_response.status.should == 200
105+
end
106+
107+
it 'should coerce a file' do
108+
post('/file', :uploaded_file => Rack::Test::UploadedFile.new(__FILE__))
109+
last_response.status.should == 201
110+
end
93111
end

0 commit comments

Comments
 (0)