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

Bugfix: values without value breaks type and default #1615

Merged
merged 3 commits into from
Apr 14, 2017

Conversation

jlfaber
Copy link
Contributor

@jlfaber jlfaber commented Apr 13, 2017

The checking of type and default validators in params_scope.rb is broken in cases where values is a Hash containing only the except attribute (and no value attribute). For example both of the following are broken:

params do
  requires :foo, type: String, values: { except: ['bar', 'baz'] }
  requires :foo2, values: { except: ['bar2', 'baz2'] }, default: 'bing'
end

if values.is_a? Hash
excepts = values[:except]
values = values[:value]
values = nil if values.is_a? Proc
Copy link
Member

@dblock dblock Apr 14, 2017

Choose a reason for hiding this comment

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

This is a little hard to read and resets values with proc after assigning it. Maybe flatten this thing, if I understand correctly you can do something like this:

values = options_key?(:values, :value, validations) ? validations[:values][:value] : validations[:values]
excepts = ...

Give it a try?

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'm looking for a little different behavior that's tricky with a one-liner. I really don't ever want values to be a Hash. But that's what the one-liner above will do if the Hash doesn't have a value attribute. Fair point about reassigning variables though. I'll push another commit that clarifies it a bit (I hope).

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 should add that, tbh, I'm not a fan of the current syntax for except. After playing around with it, I think we'd be better served by a separate top level validator to handle value exclusion. But in the mean time, the current syntax should at least work properly. ;)

@dblock
Copy link
Member

dblock commented Apr 14, 2017

Better, thx.

@dblock dblock merged commit ee62ea5 into ruby-grape:master Apr 14, 2017
@jlfaber jlfaber deleted the except_fix branch April 14, 2017 16:10
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.

2 participants