Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allows :in, :within, :range validations to be used on Array params #65

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -126,9 +126,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