Skip to content

Commit

Permalink
skip validation for a file if it is optional and nil (#1977)
Browse files Browse the repository at this point in the history
  • Loading branch information
dnesteryuk committed Jan 16, 2020
1 parent cacf82e commit 348a7c3
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#### Fixes

* [#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).
* [#1976](https://github.com/ruby-grape/grape/pull/1976): Ensure classes/modules listed for autoload really exist - [@dnesteryuk](https://github.com/dnesteryuk).
* [#1971](https://github.com/ruby-grape/grape/pull/1971): Fix BigDecimal coercion - [@FlickStuart](https://github.com/FlickStuart).
* [#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).
Expand Down
1 change: 1 addition & 0 deletions lib/grape/validations/types/file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module Types
# this class is here only to assert that rack's handling has succeeded.
class File
def call(input)
return if input.nil?
return InvalidValue.new unless coerced?(input)

# Processing of multipart file objects
Expand Down
14 changes: 5 additions & 9 deletions spec/grape/validations_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,17 @@ def app

describe 'params' do
context 'optional' do
it 'validates when params is present' do
before do
subject.params do
optional :a_number, regexp: /^[0-9]+$/
optional :attachment, type: File
end
subject.get '/optional' do
'optional works!'
end
end

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

it "doesn't validate when param not present" do
subject.params do
optional :a_number, regexp: /^[0-9]+$/
end
subject.get '/optional' do
'optional works!'
end

get '/optional'
get '/optional', a_number: nil, attachment: nil
expect(last_response.status).to eq(200)
expect(last_response.body).to eq('optional works!')
end
Expand Down

0 comments on commit 348a7c3

Please sign in to comment.