Skip to content

Commit

Permalink
Merge pull request #52 from bsm/fix/per-page-comparison
Browse files Browse the repository at this point in the history
Skip per-page range validation for non-integer values
  • Loading branch information
dim authored Jun 18, 2020
2 parents e08613b + ca89e96 commit da583fa
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
grape-kaminari (0.2.0)
grape-kaminari (0.2.1)
grape (>= 1.0)
kaminari-grape

Expand Down
2 changes: 1 addition & 1 deletion lib/grape/kaminari/max_value_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module Kaminari
class MaxValueValidator < Grape::Validations::Base
def validate_param!(attr_name, params)
attr = params[attr_name]
return unless attr && @option && attr > @option
return unless attr.is_a?(Integer) && @option && attr > @option

raise Grape::Exceptions::Validation.new(
params: [@scope.full_name(attr_name)],
Expand Down
2 changes: 1 addition & 1 deletion lib/grape/kaminari/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Grape
module Kaminari
VERSION = '0.2.0'.freeze
VERSION = '0.2.1'.freeze
end
end
10 changes: 8 additions & 2 deletions spec/kaminari_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,15 +99,21 @@ class PaginatedAPI < Grape::API

it 'succeeds when :per_page is within :max_value' do
get('/', page: 1, per_page: 999)
expect(last_response.status).to eq 200
expect(last_response.status).to eq(200)
end

it 'ensures :per_page is within :max_value' do
get('/', page: 1, per_page: 1_000)
expect(last_response.status).to eq 400
expect(last_response.status).to eq(400)
expect(last_response.body).to match(/per_page must be less than or equal 999/)
end

it 'ensures :per_page is numeric' do
get('/', page: 1, per_page: 'foo')
expect(last_response.status).to eq(400)
expect(last_response.body).to match(/per_page is invalid/)
end

it 'defaults :offset to customized value' do
expect(params['offset'][:default]).to eq(9)
end
Expand Down

0 comments on commit da583fa

Please sign in to comment.