Skip to content

Commit

Permalink
Merge pull request #2323 from dhruvCW/endless_range
Browse files Browse the repository at this point in the history
fix using endless ranges for values parameter.
  • Loading branch information
dblock committed May 8, 2023
2 parents 4efe0d6 + a2f5a8c commit 1f876b2
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
* [#2307](https://github.com/ruby-grape/grape/pull/2307): Fixed autoloading of InvalidValue - [@fixlr](https://github.com/fixlr).
* [#2315](https://github.com/ruby-grape/grape/pull/2315): Update rspec - [@ericproulx](https://github.com/ericproulx).
* [#2319](https://github.com/ruby-grape/grape/pull/2319): Update rubocop - [@ericproulx](https://github.com/ericproulx).
* [#2323](https://github.com/ruby-grape/grape/pull/2323): Fix using endless ranges for values parameter - [@dhruvCW](https://github.com/dhruvCW).
* Your contribution here.

### 1.7.0 (2022/12/20)
Expand Down
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1587,6 +1587,15 @@ params do
end
```

Note endless ranges are also supported but they require that the type be provided.

```ruby
params do
requires :minimum, type: Integer, values: 10..
optional :maximum, type: Integer, values: ..10
end
```

Note that *both* range endpoints have to be a `#kind_of?` your `:type` option (if you don't supply the `:type` option, it will be guessed to be equal to the class of the range's first endpoint). So the following is invalid:

```ruby
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 @@ -485,7 +485,7 @@ def validate_value_coercion(coerce_type, *values_list)
values_list.each do |values|
next if !values || values.is_a?(Proc)

value_types = values.is_a?(Range) ? [values.begin, values.end] : values
value_types = values.is_a?(Range) ? [values.begin, values.end].compact : values
value_types = value_types.map { |type| Grape::API::Boolean.build(type) } if coerce_type == Grape::API::Boolean
raise Grape::Exceptions::IncompatibleOptionValues.new(:type, coerce_type, :values, values) unless value_types.all?(coerce_type)
end
Expand Down
19 changes: 19 additions & 0 deletions spec/grape/validations/validators/values_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,13 @@ def include?(value)
{ type: params[:type] }
end

params do
optional :type, type: Integer, values: 1..
end
get '/endless' do
{ type: params[:type] }
end

params do
requires :type, values: ->(v) { ValuesModel.include? v }
end
Expand Down Expand Up @@ -374,6 +381,18 @@ def include?(value)
expect(last_response.body).to eq({ error: 'type does not have a valid value' }.to_json)
end

it 'validates against values in an endless range' do
get('/endless', type: 10)
expect(last_response.status).to eq 200
expect(last_response.body).to eq({ type: 10 }.to_json)
end

it 'does not allow an invalid value for a parameter using an endless range' do
get('/endless', type: 0)
expect(last_response.status).to eq 400
expect(last_response.body).to eq({ error: 'type does not have a valid value' }.to_json)
end

it 'does not allow non-numeric string value for int value using lambda' do
get('/lambda_int_val', number: 'foo')
expect(last_response.status).to eq 400
Expand Down

0 comments on commit 1f876b2

Please sign in to comment.