Skip to content

Commit 348a7c3

Browse files
authored
skip validation for a file if it is optional and nil (#1977)
1 parent cacf82e commit 348a7c3

File tree

3 files changed

+7
-9
lines changed

3 files changed

+7
-9
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#### Fixes
88

9+
* [#1977](https://github.com/ruby-grape/grape/pull/1977): Skip validation for a file if it is optional and nil - [@dnesteryuk](https://github.com/dnesteryuk).
910
* [#1976](https://github.com/ruby-grape/grape/pull/1976): Ensure classes/modules listed for autoload really exist - [@dnesteryuk](https://github.com/dnesteryuk).
1011
* [#1971](https://github.com/ruby-grape/grape/pull/1971): Fix BigDecimal coercion - [@FlickStuart](https://github.com/FlickStuart).
1112
* [#1968](https://github.com/ruby-grape/grape/pull/1968): Fix args forwarding in Grape::Middleware::Stack#merge_with for ruby 2.7.0 - [@dm1try](https://github.com/dm1try).

lib/grape/validations/types/file.rb

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ module Types
88
# this class is here only to assert that rack's handling has succeeded.
99
class File
1010
def call(input)
11+
return if input.nil?
1112
return InvalidValue.new unless coerced?(input)
1213

1314
# Processing of multipart file objects

spec/grape/validations_spec.rb

+5-9
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,17 @@ def app
1111

1212
describe 'params' do
1313
context 'optional' do
14-
it 'validates when params is present' do
14+
before do
1515
subject.params do
1616
optional :a_number, regexp: /^[0-9]+$/
17+
optional :attachment, type: File
1718
end
1819
subject.get '/optional' do
1920
'optional works!'
2021
end
22+
end
2123

24+
it 'validates when params is present' do
2225
get '/optional', a_number: 'string'
2326
expect(last_response.status).to eq(400)
2427
expect(last_response.body).to eq('a_number is invalid')
@@ -29,14 +32,7 @@ def app
2932
end
3033

3134
it "doesn't validate when param not present" do
32-
subject.params do
33-
optional :a_number, regexp: /^[0-9]+$/
34-
end
35-
subject.get '/optional' do
36-
'optional works!'
37-
end
38-
39-
get '/optional'
35+
get '/optional', a_number: nil, attachment: nil
4036
expect(last_response.status).to eq(200)
4137
expect(last_response.body).to eq('optional works!')
4238
end

0 commit comments

Comments
 (0)