Skip to content

Commit

Permalink
Merge branch 'master' into active_support_functions
Browse files Browse the repository at this point in the history
  • Loading branch information
ericproulx committed May 9, 2023
2 parents c7d8310 + 6145cd4 commit 84a03c7
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/edge.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
name: edge
on: pull_request
on: workflow_dispatch
jobs:
test:
strategy:
Expand Down
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
* [#2310](https://github.com/ruby-grape/grape/pull/2310): Fix YARD docs markdown rendering - [@duffn](https://github.com/duffn).
* [#2317](https://github.com/ruby-grape/grape/pull/2317): Remove maruku and rubocop-ast as direct development/testing dependencies - [@ericproulx](https://github.com/ericproulx).
* [#2292](https://github.com/ruby-grape/grape/pull/2292): Introduce Docker to local development - [@ericproulx](https://github.com/ericproulx).
* [#2325](https://github.com/ruby-grape/grape/pull/2325): Change edge test workflows only run on demand - [@dblock](https://github.com/dblock).
* [#2326](https://github.com/ruby-grape/grape/pull/2326): Use ActiveSupport extensions - [@ericproulx](https://github.com/ericproulx).

* Your contribution here.

#### Fixes
Expand All @@ -21,6 +23,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 with ActiveSupport >= 6.0, 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', if: ActiveSupport::VERSION::MAJOR >= 6 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', if: ActiveSupport::VERSION::MAJOR >= 6 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 84a03c7

Please sign in to comment.