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

Reject hexadecimal numbers with signs while validating numericality #39014

merged 1 commit into from
Apr 22, 2020

Conversation

nimish-mehta
Copy link
Contributor

Given the following class definition

class User < ApplicationRecord
  validates :email, presence: true
  validates :age, numericality: true
end

Currently '0x11' is validated as not a number. But '0x11' and '-0x11' pass the validation but parsed value is 0 and -0 respectively.

irb(main):004:0> User
=> User(id: integer, email: text, age: decimal, created_at: datetime, updated_at: datetime)

irb(main):008:0> u = User.new(email: 'abc', age: '0x11')
=> #<User id: nil, email: "abc", age: 0.0, created_at: nil, updated_at: nil>
irb(main):009:0> u.valid?
=> false
irb(main):010:0> u.errors.full_messages
=> ["Age is not a number"]

irb(main):011:0> u = User.new(email: 'abc', age: '+0x11')
=> #<User id: nil, email: "abc", age: 0.0, created_at: nil, updated_at: nil>
irb(main):012:0> u.valid?
=> true


irb(main):013:0> u = User.new(email: 'abc', age: '-0x11')
=> #<User id: nil, email: "abc", age: -0.0, created_at: nil, updated_at: nil>
irb(main):014:0> u.valid?
=> true

@eugeneius eugeneius merged commit d796193 into rails:master Apr 22, 2020
@eugeneius
Copy link
Member

Thanks! This regressed in #35010, as previously BigDecimal() would have rejected those values.

eugeneius added a commit that referenced this pull request Apr 22, 2020
…-literals

Reject hexadecimal numbers with signs while validating numericality
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants