Add errors for invalid input with number localization enabled

This commit is contained in:
James Wu
2023-03-03 00:29:22 +09:00
committed by David Cook
parent 824d4adf21
commit 7af26e6579
3 changed files with 24 additions and 1 deletions

View File

@@ -44,7 +44,7 @@ module Spree
def self.valid_localizable_number?(number)
return true unless number.is_a?(String) || number.respond_to?(:to_d)
return false if number.to_s =~ /[.,]\d{2}[.,]/
return false if number.to_s =~ /[.,]\d{2}[.,]/ || number.to_s =~ /[^0-9,.]+/
true
end

View File

@@ -59,5 +59,11 @@ describe Spree::LocalizedNumber do
expect(described_class.valid_localizable_number?(1599.99)).to eql true
end
end
context "with letters" do
it "returns false" do
expect(described_class.valid_localizable_number?('invalid')).to eql false
end
end
end
end

View File

@@ -45,4 +45,21 @@ shared_examples "a parent model that has a Calculator" do |parent_name|
expect(error_messages).not_to include(/^Calculator preferred/)
end
end
context "when number localization is enabled and the associated Calculator is invalid" do
let(:localized_parent) do
build(parent_name, calculator: Calculator::FlatRate.new(preferred_amount: "invalid"))
end
before do
allow(Spree::Config).to receive(:enable_localized_number?).and_return true
localized_parent.valid?
end
it "adds custom error messages to base" do
expect(localized_parent.errors[:base]).to include(
/#{I18n.t('spree.localized_number.invalid_format')}/
)
end
end
end