Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Modify the Hash#assert_valid_keys error message so that it shows the …

…valid keys. Also, show the wrong value as it was entered.

{ :failore => "stuff", :funny => "business" }.assert_valid_keys([ :failure, :funny ])
=> ArgumentError: Unknown key: failore
{ 'failore' => "stuff", :funny => "business" }.assert_valid_keys([ :failure, :funny ])
=> ArgumentError: Unknown key: failore

{ 'failore' => "stuff", :funny => "business" }.assert_valid_keys([ :failure, :funny ])
=> ArgumentError: Unknown key: "failore". Valid keys are: :failure, :funny

{ :failore => "stuff", :funny => "business" }.assert_valid_keys([ :failure, :funny ])
=> ArgumentError: Unknown key: :failore. Valid keys are: :failure, :funny

Conflicts:
	activerecord/CHANGELOG.md

Closes #11624.
  • Loading branch information...
commit 2ebf47aea21ff8ac10681e53e78dd7a0e5c31c6e 1 parent 6e905e2
@Nerian Nerian authored carlosantoniodasilva committed
View
5 activesupport/CHANGELOG.md
@@ -1,3 +1,8 @@
+* Show valid keys when `assert_valid_keys` raises an exception, and show the
+ wrong value as it was entered.
+
+ *Gonzalo Rodríguez-Baltanás Díaz*
+
* Both `cattr_*` and `mattr_*` method definitions now live in `active_support/core_ext/module/attribute_accessors`.
Requires to `active_support/core_ext/class/attribute_accessors` are
View
6 activesupport/lib/active_support/core_ext/hash/keys.rb
@@ -61,13 +61,13 @@ def symbolize_keys!
# on a mismatch. Note that keys are NOT treated indifferently, meaning if you
# use strings for keys but assert symbols as keys, this will fail.
#
- # { name: 'Rob', years: '28' }.assert_valid_keys(:name, :age) # => raises "ArgumentError: Unknown key: years"
- # { name: 'Rob', age: '28' }.assert_valid_keys('name', 'age') # => raises "ArgumentError: Unknown key: name"
+ # { name: 'Rob', years: '28' }.assert_valid_keys(:name, :age) # => raises "ArgumentError: Unknown key: :years. Valid keys are: :name, :age"
+ # { name: 'Rob', age: '28' }.assert_valid_keys('name', 'age') # => raises "ArgumentError: Unknown key: :name. Valid keys are: 'name', 'age'"
# { name: 'Rob', age: '28' }.assert_valid_keys(:name, :age) # => passes, raises nothing
def assert_valid_keys(*valid_keys)
valid_keys.flatten!
each_key do |k|
- raise ArgumentError.new("Unknown key: #{k}") unless valid_keys.include?(k)
+ raise ArgumentError.new("Unknown key: #{k.inspect}. Valid keys are: #{valid_keys.map(&:inspect).join(', ')}") unless valid_keys.include?(k)
end
end
View
2  activesupport/test/core_ext/hash_ext_test.rb
@@ -624,7 +624,7 @@ def test_assert_valid_keys
{ :failure => "stuff", :funny => "business" }.assert_valid_keys(:failure, :funny)
end
- assert_raise(ArgumentError, "Unknown key: failore") do
+ assert_raise(ArgumentError, "Unknown key: :failore. Valid keys are: :failure, :funny") do
{ :failore => "stuff", :funny => "business" }.assert_valid_keys([ :failure, :funny ])
{ :failore => "stuff", :funny => "business" }.assert_valid_keys(:failure, :funny)
end
Please sign in to comment.
Something went wrong with that request. Please try again.