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

do not raise an IncompatibleOptionValues For a couple of potentially valid use cases #1252

Merged
merged 1 commit into from
Jan 22, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* [#1237](/~https://github.com/ruby-grape/grape/pull/1237): Allow multiple parameters in `given`, which behaves as if the scopes were nested in the inputted order - [@ochagata](/~https://github.com/ochagata).
* [#1238](/~https://github.com/ruby-grape/grape/pull/1238): Call `after` of middleware on error - [@namusyaka](/~https://github.com/namusyaka).
* [#1243](/~https://github.com/ruby-grape/grape/pull/1243): Add `header` support for middleware - [@namusyaka](/~https://github.com/namusyaka).
* [#1252](/~https://github.com/ruby-grape/grape/pull/1252): Allow default to be a subset or equal to allowed values without raising IncompatibleOptionValues - [@jeradphelps](/~https://github.com/jeradphelps).
* Your contribution here.

#### Fixes
Expand Down
2 changes: 1 addition & 1 deletion lib/grape/validations/params_scope.rb
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ def guess_coerce_type(coerce_type, values)
def check_incompatible_option_values(values, default)
return unless values && default
return if values.is_a?(Proc) || default.is_a?(Proc)
return if values.include?(default)
return if values.include?(default) || (Array(default) - Array(values)).empty?
fail Grape::Exceptions::IncompatibleOptionValues.new(:default, default, :values, values)
end

Expand Down
18 changes: 18 additions & 0 deletions spec/grape/validations/params_scope_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,24 @@ def initialize(value)
end
end

context 'when the default is an array' do
context 'and is the entire range of allowed values' do
it 'does not raise an exception' do
expect do
subject.params { optional :numbers, type: Array[Integer], values: 0..2, default: 0..2 }
end.to_not raise_error
end
end

context 'and is a subset of allowed values' do
it 'does not raise an exception' do
expect do
subject.params { optional :numbers, type: Array[Integer], values: [0, 1, 2], default: [1, 0] }
end.to_not raise_error
end
end
end

context 'when both range endpoints are #kind_of? the type' do
it 'accepts values in the range' do
subject.params do
Expand Down