Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Skip validation for a file if it is optional and nil #1977

Merged
merged 1 commit into from
Jan 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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