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

BOOLEAN_ALLOWS_BOOLEAN_MESSAGE warning is invalid #761

Closed
mhuggins opened this issue Jul 21, 2015 · 11 comments
Closed

BOOLEAN_ALLOWS_BOOLEAN_MESSAGE warning is invalid #761

mhuggins opened this issue Jul 21, 2015 · 11 comments

Comments

@mhuggins
Copy link

I have a custom ActiveModel::Model in which I'm validating true/false inclusion:

# model
validates :is_org, inclusion: { in: [true, false] }

# spec
it { is_expected.to validate_inclusion_of(:is_org).in_array([true, false]) }

This results in the BOOLEAN_ALLOWS_BOOLEAN_MESSAGE warning being output during my tests run:


Warning from shoulda-matchers:

You are using validate_inclusion_of to assert that a boolean column
allows boolean values and disallows non-boolean ones. Be aware that it
is not possible to fully test this, as boolean columns will
automatically convert non-boolean values to boolean ones. Hence, you
should consider removing this test.

The problem with this is that the warning is only valid for ActiveRecord, Mongoid, or other ORMs that translate values to true or false. If just using an ActiveModel or other bare-bones model, this is not true.

This can be demonstrated by simply adding an allow_value matcher to the suite:

# spec
it { is_expected.to allow_value('true').for(:is_org) }

# output
Failure/Error: it { is_expected.to allow_value('true').for(:is_org) }
  Did not expect errors when is_org is set to "true",
  got errors:
  * "is not included in the list" (attribute: is_org, value: "true")

This warning should go away, or it should have an option to disable it since it's incorrectly mucking up my test output.

@mcmire
Copy link
Collaborator

mcmire commented Jul 21, 2015

You're right, and I didn't think about that.

I'm not sure when I'll be able to get to this, but I'll leave it open until then.

@mhuggins
Copy link
Author

I don't mind finding the time to get a PR together as well, happy to help!
On Jul 21, 2015 5:38 PM, "Elliot Winkler" notifications@github.com wrote:

You're right, and I didn't think about that.

I'm not sure when I'll be able to get to this, but I'll leave it open
until then.


Reply to this email directly or view it on GitHub
#761 (comment)
.

@backus
Copy link

backus commented Nov 18, 2015

+1

1 similar comment
@OfTheDelmer
Copy link

+1

@mcmire
Copy link
Collaborator

mcmire commented Oct 19, 2016

I think we should probably remove this warning in the following cases:

  • If the model is not an ActiveRecord model
  • If the model is an ActiveRecord model, but the column is nullable

The Rails documentation has this to say under validates_presence_of:

If you want to validate the presence of a boolean field (where the real values are true and false), you will want to use validates_inclusion_of :field_name, in: [true, false].

This is technically true, but it will have no effect if either of the conditions above are false.

@mcmire
Copy link
Collaborator

mcmire commented May 6, 2020

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!

@mcmire mcmire closed this as completed May 6, 2020
@cbortz
Copy link

cbortz commented Sep 21, 2021

If you feel that we should reopen this issue, then please let us know so that we can reprioritize it.

I'd love to see this come back. I've hit the same issue where a pure ActiveModel (i.e. non-persisted model class) sets this validation. We'd love to clean up our test suite output to not complain about this valid (in our case and others) implementation.

I'd be happy to open up a PR! Please let me know, and thank you for your consideration 🙏

@mcmire
Copy link
Collaborator

mcmire commented Sep 21, 2021

@cbortz Yes, please open a PR!

@cbortz
Copy link

cbortz commented Sep 21, 2021

@mcmire Will do! Would y'all prefer this to be:

  • Automatically disabled based on the class ancestry?
  • Configurable via Shoulda::Matchers.configure?
  • Both?
  • Something else?

I will go ahead and get started on something today regardless. But if there are any preferences or requirements, please let me know! 🙏

@mcmire
Copy link
Collaborator

mcmire commented Sep 21, 2021

@cbortz I think it should be based on class ancestry. I doubt that most people use the Attributes API in ActiveModel models, so it is very rare that boolean values will be converted in such a way that the error message indicates. I'll repeat what I said up earlier:

I think we should probably remove this warning in the following cases:

  • If the model is not an ActiveRecord model
  • If the model is an ActiveRecord model, but the column is nullable

@cbortz
Copy link

cbortz commented Sep 21, 2021

Excellent. Thank you for clarifying.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants