1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Fix Regression in Numericality Validations

A regression (#22744) introduced in 7500dae 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]
This commit is contained in:
Robert Eshleman 2015-12-22 13:29:54 -05:00
parent b96fdd234d
commit 9c330798b0

View file

@ -39,6 +39,10 @@ module ActiveModel
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 @@ module ActiveModel
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