-
Notifications
You must be signed in to change notification settings - Fork 85
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
Validate all elements of ArrayType #46
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @bostanio, thank you for the contribution 💪 Looks good to me, please take a look at my suggestion but the decision is all yours. Also, could you please add yourself to the changelog?
@@ -24,7 +24,7 @@ def validate_each(record, attribute, value) | |||
when :json | |||
strategy.call(attribute, record.errors, value.errors) if value.invalid? | |||
when :array | |||
record.errors.add(attribute, :invalid) if value.any?(&:invalid?) | |||
record.errors.add(attribute, :invalid) if value.select(&:invalid?).present? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
record.errors.add(attribute, :invalid) if value.select(&:invalid?).present? | |
record.errors.add(attribute, :invalid) if value.select(&:invalid?).any? |
I guess we still can use .any?
just to make the line shorter
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Personally I'd use present?
as we care that the result is non-empty or not. Not much in it either way.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In this particular case .select
cannot return nil
🙂
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
exactly 😁
Any chance we can do a point release of 0.6.2 for this? |
Sure, I've just rolled it out to RubyGems |
The previous implementation used
any?
to validate ArrayType. That has the effect elements after the first invalid element don't get validated and their errors collection isn't populated.This fix makes sure to call
invalid?
on every element of the array before setting an error on the parent record.