Skip to content

Commit

Permalink
allows :in, :within, :range validations to be used on Array params
Browse files Browse the repository at this point in the history
fixes Travis build failing by updating Bundler

see travis-ci/travis-ci#5239
and rubygems/bundler#3559
  • Loading branch information
janfoeh authored and adrianbn committed Jun 4, 2017
1 parent a74df8c commit 15fbd9e
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
before_install:
- gem install bundler
14 changes: 12 additions & 2 deletions lib/sinatra/param.rb
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,19 @@ def validate!(param, options)
when :in, :within, :range
raise InvalidParameterError, "Parameter must be within #{value}" unless param.nil? || case value
when Range
value.include?(param)
case param
when Array
param.all? { |element| value.include?(element) }
else
value.include?(param)
end
else
Array(value).include?(param)
case param
when Array
(param - Array(value)).empty?
else
Array(value).include?(param)
end
end
when :min
raise InvalidParameterError, "Parameter cannot be less than #{value}" unless param.nil? || value <= param
Expand Down
15 changes: 15 additions & 0 deletions spec/dummy/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -155,16 +155,31 @@ class App < Sinatra::Base
params.to_json
end

get '/validation/in/array' do
param :arg, Array, in: ['ASC', 'DESC']
params.to_json
end

get '/validation/within' do
param :arg, Integer, within: 1..10
params.to_json
end

get '/validation/within/array' do
param :arg, Array, within: 1..10, transform: ->(a) { a.map(&:to_i) }
params.to_json
end

get '/validation/range' do
param :arg, Integer, range: 1..10
params.to_json
end

get '/validation/range/array' do
param :arg, Array, range: 1..10, transform: ->(a) { a.map(&:to_i) }
params.to_json
end

get '/validation/min' do
param :arg, Integer, min: 12
params.to_json
Expand Down
45 changes: 45 additions & 0 deletions spec/parameter_validations_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,21 @@
expect(response.status).to eq(200)
end
end

describe 'for Arrays' do
it 'returns 400 on requests with any value not in the set' do
get('/validation/in/array', arg: 'MISC,ASC') do |response|
expect(response.status).to eq(400)
expect(JSON.parse(response.body)['message']).to eq("Parameter must be within [\"ASC\", \"DESC\"]")
end
end

it 'returns 200 on requests with all values in the set' do
get('/validation/in/array', arg: 'ASC,DESC') do |response|
expect(response.status).to eq(200)
end
end
end
end

describe 'within' do
Expand All @@ -115,6 +130,21 @@
expect(response.status).to eq(200)
end
end

describe 'for Arrays' do
it 'returns 400 on requests with any value outside the range' do
get('/validation/within/array', arg: [20,5]) do |response|
expect(response.status).to eq(400)
expect(JSON.parse(response.body)['message']).to eq("Parameter must be within 1..10")
end
end

it 'returns 200 on requests with all values within the range' do
get('/validation/within/array', arg: [5,10]) do |response|
expect(response.status).to eq(200)
end
end
end
end

describe 'range' do
Expand All @@ -130,6 +160,21 @@
expect(response.status).to eq(200)
end
end

describe 'for Arrays' do
it 'returns 400 on requests with any value outside the range' do
get('/validation/range/array', arg: [20,5]) do |response|
expect(response.status).to eq(400)
expect(JSON.parse(response.body)['message']).to eq("Parameter must be within 1..10")
end
end

it 'returns 200 on requests with all values within the range' do
get('/validation/range/array', arg: [5,10]) do |response|
expect(response.status).to eq(200)
end
end
end
end

describe 'min' do
Expand Down

0 comments on commit 15fbd9e

Please sign in to comment.