Skip to content

Commit

Permalink
Merge pull request #1252 from jeradphelps/default-equals-or-subset-of…
Browse files Browse the repository at this point in the history
…-range

do not raise an IncompatibleOptionValues For a couple of potentially valid use cases
  • Loading branch information
dblock committed Jan 22, 2016
2 parents 6505486 + 191f1a9 commit fbf633d
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
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

0 comments on commit fbf633d

Please sign in to comment.