diff --git a/lib/mini_defender/rules/integer.rb b/lib/mini_defender/rules/integer.rb index cdafb72..a1ecc9e 100644 --- a/lib/mini_defender/rules/integer.rb +++ b/lib/mini_defender/rules/integer.rb @@ -54,9 +54,10 @@ def passes?(attribute, value, validator) end # Remove leading zero so Integer will not treat it as octal + # Handle leading zeros while preserving both + and - signs value = value .to_s - .gsub(/^0+/, '') + .gsub(/^([+-])?0+(?=\d)/, '\1') if @mode == 'relaxed' value = normalize_digits(value) diff --git a/test/rules/integer_test.rb b/test/rules/integer_test.rb index bd63eda..69dec60 100644 --- a/test/rules/integer_test.rb +++ b/test/rules/integer_test.rb @@ -62,4 +62,50 @@ def test_relaxed_passes_with_mixed_digits def test_passes_with_integer_with_leading_zero assert @rule_relax.passes?('amount', '08', nil) end + + def test_passes_with_single_zero + assert @rule.passes?('amount', '0', nil) + assert_equal 0, @rule.coerce('0') + end + + def test_passes_with_multiple_zeros + assert @rule.passes?('amount', '00', nil) + assert_equal 0, @rule.coerce('00') + end + + # Test for leading zeros before other digits + def test_removes_leading_zeros_before_digits + assert @rule.passes?('amount', '01', nil) + assert_equal 1, @rule.coerce('01') + + assert @rule.passes?('amount', '0123', nil) + assert_equal 123, @rule.coerce('0123') + + assert @rule.passes?('amount', '00123', nil) + assert_equal 123, @rule.coerce('00123') + end + + def test_handles_zeros_with_whitespace + assert @rule.passes?('amount', ' 0 ', nil) + assert_equal 0, @rule.coerce(' 0 ') + + assert @rule.passes?('amount', ' 00 ', nil) + assert_equal 0, @rule.coerce(' 00 ') + end + + def test_handles_negative_numbers_with_leading_zeros + assert @rule.passes?('amount', '-001', nil) + assert_equal(-1, @rule.coerce('-001')) + + assert @rule.passes?('amount', '-00123', nil) + assert_equal(-123, @rule.coerce('-00123')) + end + + def test_handles_positve_numbers_with_leading_zeros + assert @rule.passes?('amount', '+001', nil) + assert_equal(1, @rule.coerce('+001')) + + assert @rule.passes?('amount', '+00123', nil) + assert_equal(123, @rule.coerce('+00123')) + end end