Spec fix LocalisedNumber validation to allow negative number

As part of this PR https://github.com/openfoodfoundation/openfoodnetwork/pull/10329
LocalisedNumber validation was tightened, but that means negative number
were not valid anymore.

This commit has been cherry-picked after this fix has already been
applied. Now we just change the order of characters in the regex because
humans are used to reading the minus at the beginning of the number. But
this change doesn't change the logic at all.
This commit is contained in:
Gaetan Craig-Riou
2023-05-12 16:08:12 +10:00
committed by Maikel Linke
parent 0442f2da7e
commit d715b6b3bb
2 changed files with 7 additions and 1 deletions

View File

@@ -45,7 +45,7 @@ module Spree
def self.valid_localizable_number?(number)
return true unless number.is_a?(String) || number.respond_to?(:to_d)
# Invalid if only two digits between dividers, or if any non-number characters
return false if number.to_s =~ /[.,]\d{2}[.,]/ || number.to_s =~ /[^0-9,.-]+/
return false if number.to_s =~ /[.,]\d{2}[.,]/ || number.to_s =~ /[^-0-9,.]+/
true
end

View File

@@ -58,6 +58,12 @@ describe Spree::LocalizedNumber do
it "returns true" do
expect(described_class.valid_localizable_number?(1599.99)).to eql true
end
context "with a negative number" do
it "returns true" do
expect(described_class.valid_localizable_number?(-1599.99)).to eql true
end
end
end
context "with letters" do