提交 9c330798 编写于 作者: R Robert Eshleman

Fix Regression in Numericality Validations

A regression (#22744) introduced in 7500daec caused certain numericality
validations to raise an error when run against an attribute with a
string value. Previously, these validations would successfully run
against string values because the value was cast to a numeric class.

This commit resolves the regression by converting string values to
floats before performing numericality comparison validations.

[fixes #22744]
上级 b96fdd23
......@@ -39,6 +39,10 @@ def validate_each(record, attr_name, value)
return
end
if raw_value.is_a?(String)
value = parse_raw_value_as_a_number(raw_value)
end
options.slice(*CHECKS.keys).each do |option, option_value|
case option
when :odd, :even
......@@ -63,12 +67,15 @@ def validate_each(record, attr_name, value)
protected
def is_number?(raw_value)
parsed_value = Kernel.Float(raw_value) if raw_value !~ /\A0[xX]/
!parsed_value.nil?
!parse_raw_value_as_a_number(raw_value).nil?
rescue ArgumentError, TypeError
false
end
def parse_raw_value_as_a_number(raw_value)
Kernel.Float(raw_value) if raw_value !~ /\A0[xX]/
end
def is_integer?(raw_value)
/\A[+-]?\d+\z/ === raw_value.to_s
end
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册