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

implemented except in values validator #1486

Merged
merged 4 commits into from
Sep 9, 2016

Conversation

jonmchan
Copy link
Contributor

@jonmchan jonmchan commented Sep 8, 2016

This implements the feature requested in #1485 .

Some notes:

  • This PR allows for except to be used exclusively allowing all other values except what is defined in except as well as with values, defining a range and then marking a subset of the range as invalid.
  • This PR allows for custom except message outside of the regular validation fail message, but falls back to the regular validation fail message if no except_message is defined.
  • There was slight difficulty maintaining compatibility with allowing values to be passed in to values: without the value: key. Please ensure that line 5-8 of values.rb https://github.com/ruby-grape/grape/compare/master...jonmchan:feature/except?expand=1#diff-45b62b4ed0aed7ceff346c347f76570eR8 do not cause any incompatibility issues.
  • Side effect of this implementation is that values: [] accepts all passed parameters. Previous implementation, values: [] would reject every value. I figure nobody would be trying to create a parameter that would allow no value so I did not bother to maintain this functionality.

@jonmchan jonmchan force-pushed the feature/except branch 2 times, most recently from bb2370d to 66b80d7 Compare September 8, 2016 14:45
@jonmchan
Copy link
Contributor Author

jonmchan commented Sep 8, 2016

I don't know how to fix the danger error:

[!] Invalid `temporary_danger.rb` file: undefined local variable or method `changelog' for #<Danger::Dangerfile:0x0000000378e850>. Updating the Danger gem might fix the issue.

I can't even run danger on my local system:

$ bundle exec danger
bundler: failed to load command: danger (/home/jchan/.rbenv/versions/2.3.1/bin/danger)
NameError: undefined local variable or method `ci_klass' for #<Danger::Executor:0x0056436b27bbe0>
Did you mean?  class
  /home/jchan/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/danger-2.1.6/lib/danger/danger_core/executor.rb:16:in `run'
  /home/jchan/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/danger-2.1.6/lib/danger/commands/runner.rb:56:in `run'
  /home/jchan/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/claide-1.0.0/lib/claide/command.rb:334:in `run'
  /home/jchan/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/danger-2.1.6/bin/danger:5:in `<top (required)>'
  /home/jchan/.rbenv/versions/2.3.1/bin/danger:23:in `load'
  /home/jchan/.rbenv/versions/2.3.1/bin/danger:23:in `<top (required)>'

@jonmchan
Copy link
Contributor Author

jonmchan commented Sep 9, 2016

@dblock any feedback?

@dblock
Copy link
Member

dblock commented Sep 9, 2016

The danger problem fixed in #1480. Rebase.

@dblock
Copy link
Member

dblock commented Sep 9, 2016

Overall this looks great. My biggest question is whether it should be

requires :number, type: Integer, values: { value: 1..20 except: [4,13], except_message: 'includes unsafe numbers', message: 'is outside the range of numbers allowed' }

vs.

requires :number, type: Integer, values: 1..20, except: [4,13], except_message: 'includes unsafe numbers', message: 'is outside the range of numbers allowed'

The latter would be consistent with how we do all options today.

@jonmchan
Copy link
Contributor Author

jonmchan commented Sep 9, 2016

If I am understanding the difference in what you are suggesting, you are saying that:

#1.) value is omitted and #2.) the parameters are not passed into a hash to values.

However, I don't find this consistent with the currently implementation in terms of tests:

eg,

https://github.com/ruby-grape/grape/blob/master/spec/grape/validations/validators/values_spec.rb#L26

requires :type, values: { value: ValuesModel.values, message: 'value does not include in values' }

https://github.com/ruby-grape/grape/blob/master/spec/grape/validations/validators/values_spec.rb#L33

            optional :type, values: { value: -> { ValuesModel.values }, message: 'value does not include in values' }, default: 'valid-type2'

If you are interested in seeing values refactored, I can make that change, but the change you are asking is very different (and potentially breaking) to how it currently behaves.

I maintain that the current PR is more consistent to how values validator is currently implemented.

@jonmchan
Copy link
Contributor Author

jonmchan commented Sep 9, 2016

@dblock fixed all errors. This should be good to go unless you want me to refactor values.

@dblock
Copy link
Member

dblock commented Sep 9, 2016

Yes you're totally right @jonmchan. Merging this, looks great.

@dblock dblock merged commit 3540ea2 into ruby-grape:master Sep 9, 2016
@jonmchan jonmchan deleted the feature/except branch September 9, 2016 14:52
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