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

Validate all elements of ArrayType #46

Merged
merged 1 commit into from Nov 17, 2019

Conversation

jonspalmer
Copy link
Contributor

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.

Copy link
Owner

@DmitryTsepelev DmitryTsepelev left a 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?
Copy link
Owner

Choose a reason for hiding this comment

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

Suggested change
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

Copy link
Contributor Author

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.

Copy link
Owner

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 🙂

Copy link
Contributor Author

Choose a reason for hiding this comment

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

exactly 😁

@jonspalmer
Copy link
Contributor Author

Any chance we can do a point release of 0.6.2 for this?

@DmitryTsepelev
Copy link
Owner

Sure, I've just rolled it out to RubyGems

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

Successfully merging this pull request may close these issues.

None yet

2 participants