diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..7c51609 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,2 @@ +before_install: + - gem install bundler \ No newline at end of file diff --git a/lib/sinatra/param.rb b/lib/sinatra/param.rb index 3609b78..1569163 100644 --- a/lib/sinatra/param.rb +++ b/lib/sinatra/param.rb @@ -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 diff --git a/spec/dummy/app.rb b/spec/dummy/app.rb index a3ffc13..9790242 100644 --- a/spec/dummy/app.rb +++ b/spec/dummy/app.rb @@ -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 diff --git a/spec/parameter_validations_spec.rb b/spec/parameter_validations_spec.rb index af5bdf1..56d9bfc 100644 --- a/spec/parameter_validations_spec.rb +++ b/spec/parameter_validations_spec.rb @@ -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 @@ -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 @@ -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