Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reject hexadecimal numbers with signs while validating numericality #39014

Merged
merged 1 commit into from Apr 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion activemodel/lib/active_model/validations/numericality.rb
Expand Up @@ -13,6 +13,8 @@ class NumericalityValidator < EachValidator # :nodoc:

INTEGER_REGEX = /\A[+-]?\d+\z/

HEXADECIMAL_REGEX = /\A[+-]?0[xX]/

def check_validity!
keys = CHECKS.keys - [:odd, :even]
options.slice(*keys).each do |option, value|
Expand Down Expand Up @@ -106,7 +108,7 @@ def is_integer?(raw_value)
end

def is_hexadecimal_literal?(raw_value)
/\A0[xX]/.match?(raw_value.to_s)
HEXADECIMAL_REGEX.match?(raw_value.to_s)
end

def filtered_options(value)
Expand Down
Expand Up @@ -21,7 +21,7 @@ def teardown
FLOATS = [0.0, 10.0, 10.5, -10.5, -0.0001] + FLOAT_STRINGS
INTEGERS = [0, 10, -10] + INTEGER_STRINGS
BIGDECIMAL = BIGDECIMAL_STRINGS.collect! { |bd| BigDecimal(bd) }
JUNK = ["not a number", "42 not a number", "0xdeadbeef", "0xinvalidhex", "0Xdeadbeef", "00-1", "--3", "+-3", "+3-1", "-+019.0", "12.12.13.12", "123\nnot a number"]
JUNK = ["not a number", "42 not a number", "0xdeadbeef", "-0xdeadbeef", "+0xdeadbeef", "0xinvalidhex", "0Xdeadbeef", "00-1", "--3", "+-3", "+3-1", "-+019.0", "12.12.13.12", "123\nnot a number"]
INFINITY = [1.0 / 0.0]

def test_default_validates_numericality_of
Expand Down