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

fix passing string value when hash is expected #821

Merged
merged 1 commit into from
Nov 23, 2014
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 @@ -13,6 +13,7 @@
* [#809](https://github.com/intridea/grape/pull/809): Removed automatic `(.:format)` suffix on paths if you're using only one format (e.g., with `format :json`, `/path` will respond with JSON but `/path.xml` will be a 404) - [@ajvondrak](https://github.com/ajvondrak).
* [#816](https://github.com/intridea/grape/pull/816): Added ability to filter out missing params if params is a nested hash with `declared(params, include_missing: false)` - [@georgimitev](https://github.com/georgimitev).
* [#819](https://github.com/intridea/grape/pull/819): Allowed both `desc` and `description` in the params DSL - [@mzikherman](https://github.com/mzikherman).
* [#821](https://github.com/intridea/grape/pull/821): Fixed passing string value when hash is expected in params - [@rebelact](https://github.com/rebelact).
* Your contribution here.

0.9.0 (8/27/2014)
Expand Down
2 changes: 1 addition & 1 deletion lib/grape/validations/validators/allow_blank.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module Grape
module Validations
class AllowBlankValidator < Base
def validate_param!(attr_name, params)
return if @option
return if @option || !params.is_a?(Hash)

value = params[attr_name]
value = value.strip if value.respond_to?(:strip)
Expand Down
2 changes: 1 addition & 1 deletion lib/grape/validations/validators/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def initialize(attrs, options, required, scope)
def validate!(params)
attributes = AttributesIterator.new(self, @scope, params)
attributes.each do |resource_params, attr_name|
if @required || resource_params.key?(attr_name)
if @required || (resource_params.respond_to?(:key?) && resource_params.key?(attr_name))
validate_param!(attr_name, resource_params)
end
end
Expand Down
24 changes: 24 additions & 0 deletions spec/grape/validations/validators/allow_blank_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,26 @@ class API < Grape::API
end
get '/disallow_blank_required_param_in_a_required_group'

params do
requires :user, type: Hash do
requires :name, allow_blank: false
end
end
get '/disallow_string_value_in_a_required_hash_group'

params do
requires :user, type: Hash do
optional :name, allow_blank: false
end
end
get '/disallow_blank_optional_param_in_a_required_group'

params do
optional :user, type: Hash do
optional :name, allow_blank: false
end
end
get '/disallow_string_value_in_an_optional_hash_group'
end
end
end
Expand Down Expand Up @@ -129,6 +143,11 @@ def app
get '/disallow_blank_required_param_in_a_required_group', user: { name: "" }
expect(last_response.status).to eq(400)
end

it 'refuses a string value in a required hash group' do
get '/disallow_string_value_in_a_required_hash_group', user: ""
expect(last_response.status).to eq(400)
end
end

context 'as an optional param' do
Expand All @@ -141,6 +160,11 @@ def app
get '/disallow_blank_optional_param_in_a_required_group', user: { age: "29", name: "" }
expect(last_response.status).to eq(400)
end

it 'refuses a string value in an optional hash group' do
get '/disallow_string_value_in_an_optional_hash_group', user: ""
expect(last_response.status).to eq(400)
end
end
end
end