shoulda-matchers icon indicating copy to clipboard operation
shoulda-matchers copied to clipboard

Missing Support for the numericality `in:` Range Validation

Open nbibler opened this issue 3 years ago • 7 comments

There doesn't appear to be direct support for ActiveRecord's "new" numericality in: range check:

class MyRecord < ApplicationRecord
  validates :progress, numericality: { in: 0..100 }
end

Attempting to use shoulda-matcher's validate_numericality_of seems to only expose upper and lower bound checks:

it {
  is_expected.to(
    validate_numericality_of(:progress)
      .is_greater_than_or_equal_to(0)
      .is_less_than_or_equal_to(100))
}

One might think doing something like the above would work, but it doesn't because the new failure message is ["must be in 0..100"] and that doesn't match the built-in message expectations for the greater than and less than matchers.

There is a similarly unexpected failure if one uses the validate_inclusion_of matcher, instead:

it { is_expected.to(validate_inclusion_of(:progress).in_range(0..100) }

Again, the new failure message (cited above) doesn't match the matcher's expected "is not included in the list" message.

Currently, the workaround seems to be to use the validate_inclusion_of to get Range support, but with a custom message matcher:

it { is_expected.to validate_inclusion_of(:progress).in_range(0..100).with_message('must be in 0..100') }

nbibler avatar May 14 '22 16:05 nbibler

Thanks for the report! Yes, we don't currently handle this right now. PR welcome :)

mcmire avatar May 16 '22 16:05 mcmire

Hi, I'm interested in start contributing to your project. Would you know if someone is working on this issue?

andreLumor avatar Aug 02 '22 13:08 andreLumor

As far as I'm aware, no one on our side (at @envylabs) is working on it, yet.

nbibler avatar Aug 02 '22 13:08 nbibler

Hi! I noticed that the "numericality in: range" check was added on rails 7. I don't know if resolving this issue is currently possible since shoulda-matchers still don't have rails 7 support. Any thoughts on how to solve it? Thanks.

andreLumor avatar Aug 03 '22 18:08 andreLumor

Hi, @andreLumor, unfortunately, we are not supporting rails 7 at the moment. PR Welcome.

For now, I suggest creating a custom matcher and as soon as the support it's added you could create a PR with your custom matcher.

vsppedro avatar Aug 03 '22 18:08 vsppedro

Hi, @VSPPedro. Thanks for the reply! I think that starting contributing to an issue that complex would be inefficient, but I'm interested in working on it sometime soon. Would you be able to point me to a good first issue here? Thanks, and sorry for the inconvenience!

andreLumor avatar Aug 04 '22 14:08 andreLumor

Hi, @andreLumor, relax, no inconvience at all.

To be honest, I don't have one in mind at the moment. I'll try to add a tag for that.

I saw that you found one. Thank you for your contribution.

vsppedro avatar Aug 05 '22 02:08 vsppedro

Hello everyone, I'm planning to work on this next Friday, as now we have the support for Rails 7, and it'll be used on one of the projects I work on to simplify some specs.

Did anyone already start to work on this? If not, could I add a label to say this issue has a PR in progress?

Thanks for y'all attention.

matsales28 avatar Sep 26 '22 17:09 matsales28

Hi, @matsales28, hope you're doing great. I don't think anyone started working on this. I added the label that you suggested. If you think I can help you with anything let me know. Thank you!

vsppedro avatar Sep 26 '22 20:09 vsppedro