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

Fix ValuesValidator and simplify code #1567

Merged
merged 1 commit into from
Jan 31, 2017

Conversation

jlfaber
Copy link
Contributor

@jlfaber jlfaber commented Jan 31, 2017

  • Reject any input when value or proc output is empty array
  • Properly apply except to array inputs

@coveralls
Copy link

coveralls commented Jan 31, 2017

Coverage Status

Coverage increased (+0.002%) to 99.074% when pulling e422259 on jlfaber:fix_values_validator into 9d3dcf7 on ruby-grape:master.

@jlfaber
Copy link
Contributor Author

jlfaber commented Jan 31, 2017

This PR incorporates the failing test from #1566 (Thanks @faucct!) but addresses two additional issues. #1566 was focused on the case where the values Proc returned an empty array. As noted there, that should cause any input to be rejected.

I noted that the static equivalent of that case (when values: is statically set to an empty array) should behave in the same way.

As I was reviewing the code, I also noticed that the except: implementation seemed to have a bug: when validating an input array, the validator only rejected the input if ALL of the elements of the array were exceptional. Instead, it should reject the input if ANY of the elements are exceptional.

@faucct, would you mind reviewing this fix to make sure it addresses your concerns?

I also notice that travis-ci failed both this PR and #1566. I suspect that failure may not be related to these changes since it only affects a single configuration (Ruby 2.4.0 + rails_edge.gemfil).

@faucct
Copy link

faucct commented Jan 31, 2017

It sure does. I am closing my PR.

* Reject any input when value or proc output is empty array
* Properly apply except to array inputs
if !param_array.empty? && param_array.all? { |param| excepts.include?(param) }
raise Grape::Exceptions::Validation, params: [@scope.full_name(attr_name)], message: except_message
end
raise Grape::Exceptions::Validation, params: [@scope.full_name(attr_name)], message: except_message \
Copy link
Member

@dblock dblock Jan 31, 2017

Choose a reason for hiding this comment

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

You should make it a guard clause and this way you don't have to put it on multiple lines, so:

return if excepts.nil? || params_array.none? ...
raise ...

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 could do that for the values check (the second one) but not for the excepts check (the first one) since I don't want a missing excepts to skip the values check. But, honestly, I kind of like the symmetry of the two checks -- I think it makes them a little quicker to read and understand. Your call, though. If you want me to switch the second one to a return unless I'll be happy to do that. Please advise.

Copy link
Member

Choose a reason for hiding this comment

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

We can leave this as is, thx.

@coveralls
Copy link

coveralls commented Jan 31, 2017

Coverage Status

Coverage increased (+0.002%) to 99.074% when pulling 0e6f94e on jlfaber:fix_values_validator into 9d3dcf7 on ruby-grape:master.

10 similar comments
@coveralls
Copy link

Coverage Status

Coverage increased (+0.002%) to 99.074% when pulling 0e6f94e on jlfaber:fix_values_validator into 9d3dcf7 on ruby-grape:master.

@coveralls
Copy link

Coverage Status

Coverage increased (+0.002%) to 99.074% when pulling 0e6f94e on jlfaber:fix_values_validator into 9d3dcf7 on ruby-grape:master.

@coveralls
Copy link

Coverage Status

Coverage increased (+0.002%) to 99.074% when pulling 0e6f94e on jlfaber:fix_values_validator into 9d3dcf7 on ruby-grape:master.

@coveralls
Copy link

Coverage Status

Coverage increased (+0.002%) to 99.074% when pulling 0e6f94e on jlfaber:fix_values_validator into 9d3dcf7 on ruby-grape:master.

@coveralls
Copy link

Coverage Status

Coverage increased (+0.002%) to 99.074% when pulling 0e6f94e on jlfaber:fix_values_validator into 9d3dcf7 on ruby-grape:master.

@coveralls
Copy link

Coverage Status

Coverage increased (+0.002%) to 99.074% when pulling 0e6f94e on jlfaber:fix_values_validator into 9d3dcf7 on ruby-grape:master.

@coveralls
Copy link

Coverage Status

Coverage increased (+0.002%) to 99.074% when pulling 0e6f94e on jlfaber:fix_values_validator into 9d3dcf7 on ruby-grape:master.

@coveralls
Copy link

Coverage Status

Coverage increased (+0.002%) to 99.074% when pulling 0e6f94e on jlfaber:fix_values_validator into 9d3dcf7 on ruby-grape:master.

@coveralls
Copy link

Coverage Status

Coverage increased (+0.002%) to 99.074% when pulling 0e6f94e on jlfaber:fix_values_validator into 9d3dcf7 on ruby-grape:master.

@coveralls
Copy link

Coverage Status

Coverage increased (+0.002%) to 99.074% when pulling 0e6f94e on jlfaber:fix_values_validator into 9d3dcf7 on ruby-grape:master.

@jlfaber
Copy link
Contributor Author

jlfaber commented Jan 31, 2017

I tried rerunning the travis-ci build by force pushing a new commit that was identical to the original one. This one passed successfully. Which is, of course, concerning. The two builds are:

https://travis-ci.org/ruby-grape/grape/builds/197028860 (failed)

https://travis-ci.org/ruby-grape/grape/builds/197053271 (passed)

To reiterate, the source they built from was identical. @dblock any ideas what might be going on here?

@dblock
Copy link
Member

dblock commented Jan 31, 2017

@jlfaber Anything using delegators via forwardable.rb is potentially broken, see https://bugs.ruby-lang.org/issues/13107. Fix will come in Ruby 2.4.1.

@dblock dblock merged commit 081d09b into ruby-grape:master Jan 31, 2017
@dblock
Copy link
Member

dblock commented Jan 31, 2017

Merged, thank you.

@jlfaber
Copy link
Contributor Author

jlfaber commented Jan 31, 2017

You're welcome. Thanks again @faucct for the initial find and spec.

@jlfaber jlfaber deleted the fix_values_validator branch January 31, 2017 21:38
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.

4 participants