shoulda-matchers: validate_numericality_of has problems with BigDecimal in Ruby 2.4

Failure/Error:
       is_expected.to validate_numericality_of(:latitude)
         .is_greater_than_or_equal_to(-90).is_less_than_or_equal_to(90).allow_nil
     
     ArgumentError:
       invalid value for BigDecimal(): "abcd"

This exception was not thrown when doing BigDecimal('abcd') in Ruby < 2.4.0.

Ruby 2.4.0p0 Rails 5.0.1

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 28
  • Comments: 19 (1 by maintainers)

Most upvoted comments

Is there any progress on this issue? We’re still blocked from moving to Ruby 2.4.0 without dropping the shoulda-matchers

I tested on ‘my’ app and the problem occurred only at rails 4.2.8 (upgrade rails to 4.2.9 or 4.2.10 resolved issue)

should validate_numericality_of(:price)

ruby 2.4.1
minitest 5.11.3
shoulda 3.5.0
shoulda-matchers 2.8.0

@mcmire

Ruby 2.4.2, Rails 5.0.0, shoulda-matchers master, BigDecimal 1.3.2 and I get:

Variant validations should validate that :reorder_trigger looks like a number less than or equal to 2147483647
     Failure/Error: it { is_expected.to validate_numericality_of(:reorder_trigger).is_less_than_or_equal_to(2_147_483_647) }

     ArgumentError:
       invalid value for BigDecimal(): "abcd"

Same on Rails 5 branch.

Hi, I seem to have this problem under Rails 5.0.1 I’m during a process of upgrading Ruby to 2.5.3 And I just upgraded should-matchers to 4.0.1 Also using postgres

Edit: I see that the release it was fixed in (4.2.9) was released on June 26, 2017 And 5.0.1 was released on December 21, 2016 I’ll try to upgrade to 5.0.5 which was released on July 31, 2017

Edit 2: The upgrade to rails 5.0.5 solved it

This is happening because the validate_numericality_of matcher assumes that your column is a string column, not a numeric column. So it tries to use a string that doesn’t look like a number to assert that the validation is present and working: https://github.com/thoughtbot/shoulda-matchers/blob/afa6a7b6661a0d4450d330922c2c930ea2f3ff68/lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb#L501

There are actually a couple of issues reporting this same problem. See #916 and #976.

@obahareth I just updated Gemfile.lock – that updated a bunch of dependencies and it looks like it got rid of yajl-ruby, so try it again.