Skip to content

Commit

Permalink
return error on nil key and add test
Browse files Browse the repository at this point in the history
  • Loading branch information
KinWang-2013 committed Jul 19, 2024
1 parent 55c7750 commit 869460a
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 @@ -264,7 +264,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 869460a

Please sign in to comment.