Skip to content

Added support for coercion to Set or Set[Other] #995

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
Apr 20, 2015
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 @@ -3,6 +3,7 @@

#### Features

* [#995](https://github.com/intridea/grape/issues/995): Added support for coercion to Set or Set[Other] - [@jordansexton](https://github.com/jordansexton) [@u2](https://github.com/u2).
* [#980](https://github.com/intridea/grape/issues/980): Grape is now eager-loaded - [@u2](https://github.com/u2).
* [#956](https://github.com/intridea/grape/issues/956): Support `present` with `Grape::Presenters::Presenter` - [@u2](https://github.com/u2).
* [#974](https://github.com/intridea/grape/pull/974): Added `error!` to `rescue_from` blocks - [@whatasunnyday](https://github.com/whatasunnyday).
Expand Down
9 changes: 5 additions & 4 deletions lib/grape/validations/validators/coerce.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,18 @@ def _valid_single_type?(klass, val)
end

def valid_type?(val)
if @option.is_a?(Array)
_valid_array_type?(@option[0], val)
if @option.is_a?(Array) || @option.is_a?(Set)
_valid_array_type?(@option.first, val)
else
_valid_single_type?(@option, val)
end
end

def coerce_value(type, val)
# Don't coerce things other than nil to Arrays or Hashes
return val || [] if type == Array
return val || {} if type == Hash
return val || [] if type == Array
return val || Set.new if type == Set
return val || {} if type == Hash

converter = Virtus::Attribute.build(type)
converter.coerce(val)
Expand Down
66 changes: 48 additions & 18 deletions spec/grape/validations/validators/coerce_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -122,30 +122,60 @@ class User
expect(last_response.body).to eq('Fixnum')
end

it 'Array of Integers' do
subject.params do
requires :arry, coerce: Array[Integer]
end
subject.get '/array' do
params[:arry][0].class
context 'Array' do
it 'Array of Integers' do
subject.params do
requires :arry, coerce: Array[Integer]
end
subject.get '/array' do
params[:arry][0].class
end

get '/array', arry: %w(1 2 3)
expect(last_response.status).to eq(200)
expect(last_response.body).to eq('Fixnum')
end

get '/array', arry: %w(1 2 3)
expect(last_response.status).to eq(200)
expect(last_response.body).to eq('Fixnum')
end
it 'Array of Bools' do
subject.params do
requires :arry, coerce: Array[Virtus::Attribute::Boolean]
end
subject.get '/array' do
params[:arry][0].class
end

it 'Array of Bools' do
subject.params do
requires :arry, coerce: Array[Virtus::Attribute::Boolean]
get 'array', arry: [1, 0]
expect(last_response.status).to eq(200)
expect(last_response.body).to eq('TrueClass')
end
subject.get '/array' do
params[:arry][0].class
end

context 'Set' do
it 'Set of Integers' do
subject.params do
requires :set, coerce: Set[Integer]
end
subject.get '/set' do
params[:set].first.class
end

get '/set', set: Set.new([1, 2, 3, 4]).to_a
expect(last_response.status).to eq(200)
expect(last_response.body).to eq('Fixnum')
end

get 'array', arry: [1, 0]
expect(last_response.status).to eq(200)
expect(last_response.body).to eq('TrueClass')
it 'Set of Bools' do
subject.params do
requires :set, coerce: Set[Virtus::Attribute::Boolean]
end
subject.get '/set' do
params[:set].first.class
end

get '/set', set: Set.new([1, 0]).to_a
expect(last_response.status).to eq(200)
expect(last_response.body).to eq('TrueClass')
end
end

it 'Bool' do
Expand Down