Skip to content

Commit

Permalink
Don't fail even if invalid type value is passed to default validator
Browse files Browse the repository at this point in the history
Fixes #1245
  • Loading branch information
namusyaka committed Jan 19, 2016
1 parent 73dfad6 commit cc6cdb8
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

* [#1216](https://github.com/ruby-grape/grape/pull/1142): Fix JSON error response when calling `error!` with non-Strings - [@jrforrest](https://github.com/jrforrest).
* [#1225](https://github.com/ruby-grape/grape/pull/1225): Fix `given` with nested params not returning correct declared params - [@JanStevens](https://github.com/JanStevens).
* [#1249](https://github.com/ruby-grape/grape/pull/1249): Don't fail even if invalid type value is passed to default validator - [@namusyaka](https://github.com/namusyaka).

0.14.0 (12/07/2015)
===================
Expand Down
2 changes: 1 addition & 1 deletion lib/grape/validations/validators/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def validate!(params)

attrs = AttributesIterator.new(self, @scope, params)
attrs.each do |resource_params, attr_name|
if resource_params[attr_name].nil?
if resource_params.is_a?(Hash) && resource_params[attr_name].nil?
validate_param!(attr_name, resource_params)
end
end
Expand Down
15 changes: 15 additions & 0 deletions spec/grape/validations/validators/default_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,15 @@ def app
subject.post '/optional_hash_with_default_inner_params' do
{ foo_in_optional_hash: params[:optional_hash_with_default][:foo_in_optional_hash] }
end

subject.params do
optional :phone, type: Hash do
optional :zone, type: String, default: '1234'
end
end
subject.post '/optional_hash_without_default_outer_params' do
{ zone: params[:phone][:zone] }
end
end

it 'sets default value for optional hash if param is not provided' do
Expand All @@ -206,6 +215,12 @@ def app
expect(last_response.status).to eq(201)
expect(last_response.body).to eq({ foo_in_optional_hash: 'own_default' }.to_json)
end

it 'does not fail even if invalid params is provided' do
expect { post '/optional_hash_without_default_outer_params', phone: '5678' }.not_to raise_error
expect(last_response.status).to eq(400)
expect(last_response.body).to eq({ error: 'phone is invalid' }.to_json)
end
end
end
end

0 comments on commit cc6cdb8

Please sign in to comment.