Skip to content
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 @@ -8,6 +8,7 @@ Next Release
#### Fixes

* [#1038](https://github.com/intridea/grape/pull/1038): Avoid dup-ing the String class when used in inherited params - [@rnubel](https://github.com/rnubel).
* [#1042](https://github.com/intridea/grape/issues/1042): Fix coercion of complex arrays - [@dim](https://github.com/dim).

0.12.0 (6/18/2015)
==================
Expand Down
4 changes: 3 additions & 1 deletion lib/grape/validations/validators/coerce.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ def _valid_single_type?(klass, val)
end

def valid_type?(val)
if @option.is_a?(Array) || @option.is_a?(Set)
if val.instance_of?(InvalidValue)
false
elsif @option.is_a?(Array) || @option.is_a?(Set)
_valid_array_type?(@option.first, val)
else
_valid_single_type?(@option, val)
Expand Down
37 changes: 29 additions & 8 deletions spec/grape/validations/validators/coerce_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ def app
end

describe 'coerce' do
module CoerceValidatorSpec
class User
include Virtus.model
attribute :id, Integer
attribute :name, String
end
end

context 'i18n' do
after :each do
I18n.locale = :en
Expand Down Expand Up @@ -82,14 +90,6 @@ def app
end

context 'complex objects' do
module CoerceValidatorSpec
class User
include Virtus.model
attribute :id, Integer
attribute :name, String
end
end

it 'error on malformed input for complex objects' do
subject.params do
requires :user, type: CoerceValidatorSpec::User
Expand Down Expand Up @@ -148,6 +148,27 @@ class User
expect(last_response.status).to eq(200)
expect(last_response.body).to eq('TrueClass')
end

it 'Array of Complex' do
subject.params do
requires :arry, coerce: Array[CoerceValidatorSpec::User]
end
subject.get '/array' do
params[:arry].size
end

get 'array', arry: [31]
expect(last_response.status).to eq(400)
expect(last_response.body).to eq('arry is invalid')

get 'array', arry: { id: 31, name: 'Alice' }
expect(last_response.status).to eq(400)
expect(last_response.body).to eq('arry is invalid')

get 'array', arry: [{ id: 31, name: 'Alice' }]
expect(last_response.status).to eq(200)
expect(last_response.body).to eq('1')
end
end

context 'Set' do
Expand Down