-
-
Notifications
You must be signed in to change notification settings - Fork 910
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
Fix validate_length_of
when used in combination with numericality validation
#970
Comments
I've created an app to try to replicate this issue but wasnt able to. The specs passed successfully: schema.db
user.rb
user_spec.rb
I tried replicating it after I saw this spec that seems to be testing exactly what you said. Is it already fixed or did I misunderstood the issue? 😕 |
I have a similar problem: the matcher expects the numericality error message but obviously rails gives the constraint one.
The schema.rb has the following
And the failure I receive is this
It seems that the matcher doesn't know that if I do
I'm using shoulda-matchers-3.1.1, rspec-3.4.0 and rails-4.2.5 |
@jonnyjava This is a bug with the numericality matcher. Because your column is a decimal column, it will convert all given values to decimals ( Do you mind making a new issue for this so I can track it better? |
Here it is #976 |
Just wanted to note that I had a similar problem and replicated this bug. Validation: validates :unit_price_in_cents, numericality: true, length: { minimum: 2 } Tests: it { should validate_numericality_of(:unit_price_in_cents) }
it { should validate_length_of(:unit_price_in_cents).is_at_least(2) } |
@kheppenstall That's close, but the behavior you've run into isn't a bug. The |
@mcmire you're right. Although the docs for length validation don't leave that clear(I wasn't aware of this), the ones for numericality does let us infer something:
Emphasis mine. Thanks for pointing it out 👍 |
Hey folks. In an effort to lighten our load as maintainers and be able to serve you better in the future, the shoulda-matchers team is working on cleaning out the cobwebs in this repo by pruning the backlog. As there are few of us, there are a lot of items that will simply never earn our attention in a reasonable time frame, and rather than giving you an empty promise, we think it makes more sense to focus on more recent issues. That means, unfortunately, that we must close this issue. Don't take this the wrong way: our aim is not to diminish the effort people have made or dismiss problems that have been raised. If you feel that we should reopen this issue, then please let us know so that we can reprioritize it. Thanks! |
It's very common to use a combination of the numericality and length validations to validate an attribute such as a telephone number or postal code (which are usually stored as string columns).
Unfortunately, the
validate_length_of
matcher uses the string"x"
to test the length validation. When combined with the numericality validation, the matcher can never pass. Example:In this case, the
numericality
matcher will work, but thelength
matcher will fail with something like:This issue was raised in years past but has never really been addressed, it seems like.
I wonder if the way to solve this would be to add a qualifier to
validate_length_of
:The text was updated successfully, but these errors were encountered: