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

Revert #22610 by default and make it opt-in #41412

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

intrip
Copy link
Contributor

@intrip intrip commented Feb 11, 2021

Summary

Change #22610 in order to revert the behaviour by default.
If the option all: true is given applies the new behaviour.

Fixes #41051

@intrip intrip force-pushed the 41051-add-explicit-option-for-inclusion-all-when-array branch from 511333d to 3aacc11 Compare February 11, 2021 17:06
@intrip intrip force-pushed the 41051-add-explicit-option-for-inclusion-all-when-array branch 2 times, most recently from 4889b5a to 37b34fe Compare February 12, 2021 09:59
@intrip intrip changed the title WIP Revert #22610 by default and makes it opt-in Feb 12, 2021
@@ -24,7 +26,7 @@ def include?(record, value)
delimiter
end

if value.is_a?(Array)
if value.is_a?(Array) && options[:all] == true
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The idea behind the all name is that we can introduce also an any option (use .any? instead of .all?) which makes sense for the exclusion validation, check cases such as: #22610 (comment)

@intrip intrip changed the title Revert #22610 by default and makes it opt-in Revert #22610 by default and make it opt-in Feb 12, 2021
@intrip intrip force-pushed the 41051-add-explicit-option-for-inclusion-all-when-array branch 4 times, most recently from 4c108a6 to e2c2e20 Compare February 12, 2021 10:13
@@ -33,6 +34,8 @@ module HelperMethods
# with <tt>Range#cover?</tt>, otherwise with <tt>include?</tt>. When using
# a proc or lambda the instance under validation is passed as an argument.
# * <tt>:within</tt> - A synonym(or alias) for <tt>:in</tt>
# * <tt>:all</tt> - If attribute is an <tt>Array</tt> validates the inclusion of
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't document this option for validates_exclusion_of (even if is available in the code path) because I couldn't find a valid use case.

@intrip
Copy link
Contributor Author

intrip commented Feb 22, 2021

@rafaelfranca could you take a peek 🙏 ?

@hovsater
Copy link
Contributor

hovsater commented Mar 6, 2021

Sorry for being out of the loop on this one. I think this looks good. 👍

My initial implementation was actually based around an option (see the pull request description), each_object: true, but was later revamped to just look for array values based on feedback in the pull request. I knew there was a reason I chose to stick with an option to begin with. 🙂

@intrip
Copy link
Contributor Author

intrip commented Apr 8, 2021

Any chance someone from the core team could take a look at this? 🙇

@intrip intrip force-pushed the 41051-add-explicit-option-for-inclusion-all-when-array branch from 2cbb005 to fb0b09b Compare May 7, 2021 13:09
@intrip
Copy link
Contributor Author

intrip commented May 10, 2021

bump 🏓

@intrip intrip force-pushed the 41051-add-explicit-option-for-inclusion-all-when-array branch from fb0b09b to e270d5a Compare May 26, 2021 08:49
@intrip intrip force-pushed the 41051-add-explicit-option-for-inclusion-all-when-array branch from e270d5a to 56a87db Compare June 9, 2021 09:10
@intrip
Copy link
Contributor Author

intrip commented Jun 10, 2021

@zzak sorry to tag you but you're super active, maybe you can take a look a this PR too? It's been there for a while 😄

def test_validates_inclusion_of_with_array_value
Person.validates_inclusion_of :karma, in: %w( abe monkey )
def test_validates_inclusion_of_with_array_value_and_all_option_is_true
Person.validates_inclusion_of :karma, in: %w( abe monkey ), all: true
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is all required for this test to pass? Or were you modifying an existing test on purpose 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes all: true is required, this is basically the purpose of this PR: reverting the default and making this behaviour opt-in

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@intrip What happens if we remove all: true?

I'm hesitant to revert the old behavior as this likely already cost some upgrade cycles for folks, to revert that behavior may be confusing. If we kept the current behavior, is there still a way to opt-out?

Copy link
Contributor Author

@intrip intrip Jun 11, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@zzak
I understand your point, honestly I've just followed #41051 (comment) suggestion.
Changing this to opt-out is dead simple in any case and I don't mind following a more conservative approach.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will trust @rafaelfranca here, since his original suggestion was to revert to the old behavior and opt-in. 🙏

Just making sure we didn't at least consider the conservative approach first.

@zzak
Copy link
Member

zzak commented Jun 10, 2021

@intrip I took a quick look, this one tab was open all day.. The approach seems good to me? But I think it's better to wait for someone like @rafaelfranca who has more context here. 🙏

@intrip intrip force-pushed the 41051-add-explicit-option-for-inclusion-all-when-array branch from 56a87db to afa899a Compare June 25, 2021 15:30
@zzak zzak requested a review from rafaelfranca June 25, 2021 22:47
@intrip intrip force-pushed the 41051-add-explicit-option-for-inclusion-all-when-array branch from afa899a to 650623f Compare June 28, 2021 09:17
@zzak zzak removed the docs label Jun 28, 2021
@rails-bot
Copy link

rails-bot bot commented Sep 26, 2021

This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.
Thank you for your contributions.

@rails-bot rails-bot bot added the stale label Sep 26, 2021
@intrip
Copy link
Contributor Author

intrip commented Sep 26, 2021

still relevant

@rails-bot rails-bot bot removed the stale label Sep 26, 2021
@intrip intrip force-pushed the 41051-add-explicit-option-for-inclusion-all-when-array branch from 650623f to 18fe512 Compare September 26, 2021 13:32
@rails-bot
Copy link

rails-bot bot commented Dec 25, 2021

This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.
Thank you for your contributions.

@rails-bot rails-bot bot added the stale label Dec 25, 2021
@intrip
Copy link
Contributor Author

intrip commented Dec 27, 2021

ping

@rails-bot rails-bot bot removed the stale label Dec 27, 2021
@rails-bot
Copy link

rails-bot bot commented Mar 27, 2022

This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.
Thank you for your contributions.

@rails-bot rails-bot bot added the stale label Mar 27, 2022
@intrip
Copy link
Contributor Author

intrip commented Mar 29, 2022

pong

@rails-bot rails-bot bot removed the stale label Mar 29, 2022
Change rails#22610 in order to revert the behaviour by default.
If the option `all: true` is given applies the new behaviour.
@intrip intrip force-pushed the 41051-add-explicit-option-for-inclusion-all-when-array branch from 18fe512 to e481fc8 Compare August 24, 2022 21:07
@elliottmason
Copy link

Is there another way to concisely express a validation that functions the way validates :value, exclusion: { in: [nil] } did prior to #22610? It's one thing for the behavior to change, but it seems like achieving the previous behavior is basically broken right now without writing a custom validation.

@zzak
Copy link
Member

zzak commented Mar 24, 2023

@elliottmason Are you able to try this PR in your app? It would be good to know if there are any regressions before this is merged.

@elliottmason
Copy link

Thanks for the suggestion @zzak I will do that and report my findings.

@sdrioux
Copy link

sdrioux commented Jul 21, 2023

@zzak @rafaelfranca this fix has been around for awhile - any chance we could get this merged?

@sdrioux
Copy link

sdrioux commented Jul 21, 2023

@elliottmason Did you have any luck with this PR?

@xjunior
Copy link
Contributor

xjunior commented Jan 12, 2024

We're reaching 6.1 now, and this came up. Are there plans to merge this?

@xjunior
Copy link
Contributor

xjunior commented Jan 12, 2024

My current solution to this is:

-                exclusion: { in: [nil] }
+                length: { minimum: 0, allow_nil: false, message: "can't be nil" }

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.

Empty arrays don't pass validate exclusion of nil in Rails 6.1
6 participants