Skip to content

Add extra specs for Boolean type field #1650

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

Merged
merged 1 commit into from
Jul 3, 2017
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 @@ -7,6 +7,7 @@
* [#1635](https://github.com/ruby-grape/grape/pull/1635): Instrument validators with ActiveSupport::Notifications - [@ktimothy](https://github.com/ktimothy).
* [#1646](https://github.com/ruby-grape/grape/pull/1646): Add ability to include an array of modules as helpers - [@pablonahuelgomez](https://github.com/pablonahuelgomez).
* [#1623](https://github.com/ruby-grape/grape/pull/1623): Removed `multi_json` and `multi_xml` dependencies - [@dblock](https://github.com/dblock).
* [#1650](https://github.com/ruby-grape/grape/pull/1650): Add extra specs for Boolean type field - [@tiarly](https://github.com/tiarly).
* Your contribution here.

#### Fixes
Expand Down
37 changes: 37 additions & 0 deletions spec/grape/validations/validators/coerce_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,43 @@ class User
expect(last_response.body).to eq('TrueClass')
end

it 'Boolean' do
subject.params do
optional :boolean, type: Boolean, default: true
end
subject.get '/boolean' do
params[:boolean].class
end

get '/boolean'
expect(last_response.status).to eq(200)
expect(last_response.body).to eq('TrueClass')

get '/boolean', boolean: true
expect(last_response.status).to eq(200)
expect(last_response.body).to eq('TrueClass')

get '/boolean', boolean: false
expect(last_response.status).to eq(200)
expect(last_response.body).to eq('FalseClass')

get '/boolean', boolean: 'true'
expect(last_response.status).to eq(200)
expect(last_response.body).to eq('TrueClass')

get '/boolean', boolean: 'false'
expect(last_response.status).to eq(200)
expect(last_response.body).to eq('FalseClass')

get '/boolean', boolean: 123
expect(last_response.status).to eq(400)
expect(last_response.body).to eq('boolean is invalid')

get '/boolean', boolean: '123'
expect(last_response.status).to eq(400)
expect(last_response.body).to eq('boolean is invalid')
end

it 'Rack::Multipart::UploadedFile' do
subject.params do
requires :file, type: Rack::Multipart::UploadedFile
Expand Down