Skip to content
Browse files

Merge pull request #79 from rafaelss/fix_required_param_regexp

Improve the way RequiredParam validates a parameter
  • Loading branch information...
2 parents 8af1366 + afe3008 commit 1d9cbc90121aae083eae26ceb08974e36e2bad5c @igrigorik igrigorik committed
Showing with 19 additions and 2 deletions.
  1. +9 −2 lib/goliath/rack/validation/required_param.rb
  2. +10 −0 spec/unit/rack/validation/required_param_spec.rb
View
11 lib/goliath/rack/validation/required_param.rb
@@ -12,6 +12,13 @@ class RequiredParam
include Goliath::Rack::Validator
attr_reader :type, :key, :message
+ # extracted from activesupport 3.0.9
+ if defined?(Encoding) && "".respond_to?(:encode)
+ NON_WHITESPACE_REGEXP = %r![^[:space:]]!
+ else
+ NON_WHITESPACE_REGEXP = %r![^\s#{[0x3000].pack("U")}]!
+ end
+
# Creates the Goliath::Rack::Validation::RequiredParam validator
#
# @param app The app object
@@ -34,7 +41,7 @@ def call(env)
def key_valid?(params)
if !params.has_key?(key) || params[key].nil? ||
- (params[key].is_a?(String) && params[key] =~ /^\s*$/)
+ (params[key].is_a?(String) && params[key] !~ NON_WHITESPACE_REGEXP)
return false
end
@@ -42,7 +49,7 @@ def key_valid?(params)
unless params[key].compact.empty?
params[key].each do |k|
return true unless k.is_a?(String)
- return true unless k =~ /^\s*$/
+ return true unless k !~ NON_WHITESPACE_REGEXP
end
end
return false
View
10 spec/unit/rack/validation/required_param_spec.rb
@@ -91,6 +91,16 @@
@env['params']['mk'] = [1, 2, 3, 4]
@rp.key_valid?(@env['params']).should be_true
end
+
+ it "doesn't raise if the key provided is multiline and has blanks" do
+ @env['params']['mk'] = "my\n \nvalue"
+ @rp.key_valid?(@env['params']).should be_true
+ end
+
+ it "doesn't raise if the key provided is an array and contains multiline with blanks" do
+ @env['params']['mk'] = ["my\n \nvalue", "my\n \nother\n \nvalue"]
+ @rp.key_valid?(@env['params']).should be_true
+ end
end
end
end

0 comments on commit 1d9cbc9

Please sign in to comment.
Something went wrong with that request. Please try again.