Skip to content

Commit

Permalink
Merge pull request #696 from KinWang-2013/fix/exists-method-nil-key
Browse files Browse the repository at this point in the history
[FIX] Raise ArgumentError on nil key in exists?
  • Loading branch information
radar authored Sep 15, 2024
2 parents 18799df + 869460a commit 1fbc930
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/i18n.rb
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,8 @@ def interpolation_keys(key, **options)
def exists?(key, _locale = nil, locale: _locale, **options)
locale ||= config.locale
raise Disabled.new('exists?') if locale == false
raise I18n::ArgumentError if key.is_a?(String) && key.empty?
raise I18n::ArgumentError if (key.is_a?(String) && key.empty?) || key.nil?

config.backend.exists?(locale, key, options)
end

Expand Down
4 changes: 4 additions & 0 deletions test/i18n_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,10 @@ def setup
assert_raises(I18n::ArgumentError) { I18n.interpolation_keys(["bad-argument"]) }
end

test "exists? given nil raises I18n::ArgumentError" do
assert_raises(I18n::ArgumentError) { I18n.exists?(nil) }
end

test "exists? given an existing key will return true" do
assert_equal true, I18n.exists?(:currency)
end
Expand Down

0 comments on commit 1fbc930

Please sign in to comment.