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

Allow to use regexp validator with arrays #1503

Merged
merged 3 commits into from
Oct 7, 2016
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ Next Release
============

* Your contribution here.
* [#1503](/~https://github.com/ruby-grape/grape/pull/1503): Allow to use regexp validator with arrays - [@akoltun](/~https://github.com/akoltun).

0.18.0 (10/7/2016)
==================
Expand Down
3 changes: 2 additions & 1 deletion lib/grape/validations/validators/regexp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ module Grape
module Validations
class RegexpValidator < Base
def validate_param!(attr_name, params)
return unless params.key?(attr_name) && !params[attr_name].nil? && !(params[attr_name].to_s =~ (options_key?(:value) ? @option[:value] : @option))
return unless params.respond_to?(:key?) && params.key?(attr_name)
return if Array.wrap(params[attr_name]).all? { |param| param.nil? || (param.to_s =~ (options_key?(:value) ? @option[:value] : @option)) }
raise Grape::Exceptions::Validation, params: [@scope.full_name(attr_name)], message: message(:regexp)
end
end
Expand Down
88 changes: 88 additions & 0 deletions spec/grape/validations/validators/regexp_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,33 @@ class API < Grape::API
end
get do
end

params do
requires :names, type: { value: Array[String], message: 'can\'t be nil' }, regexp: { value: /^[a-z]+$/, message: 'format is invalid' }
end
get 'regexp_with_array' do
end
end

params do
requires :name, regexp: /^[a-z]+$/
end
get do
end

params do
requires :names, type: Array[String], regexp: /^[a-z]+$/
end
get 'regexp_with_array' do
end

params do
requires :people, type: Hash do
requires :names, type: Array[String], regexp: /^[a-z]+$/
end
end
get 'nested_regexp_with_array' do
end
end
end
end
Expand Down Expand Up @@ -51,6 +71,36 @@ def app
get '/custom_message', name: 'bob'
expect(last_response.status).to eq(200)
end

context 'regexp with array' do
it 'refuses inapppopriate items' do
get '/custom_message/regexp_with_array', names: ['invalid name', 'abc']
expect(last_response.status).to eq(400)
expect(last_response.body).to eq('{"error":"names format is invalid"}')
end

it 'refuses empty items' do
get '/custom_message/regexp_with_array', names: ['', 'abc']
expect(last_response.status).to eq(400)
expect(last_response.body).to eq('{"error":"names format is invalid"}')
end

it 'refuses nil items' do
get '/custom_message/regexp_with_array', names: [nil, 'abc']
expect(last_response.status).to eq(400)
expect(last_response.body).to eq('{"error":"names can\'t be nil"}')
end

it 'accepts valid items' do
get '/custom_message/regexp_with_array', names: ['bob']
expect(last_response.status).to eq(200)
end

it 'accepts nil instead of array' do
get '/custom_message/regexp_with_array', names: nil
expect(last_response.status).to eq(200)
end
end
end

context 'invalid input' do
Expand All @@ -76,4 +126,42 @@ def app
get '/', name: 'bob'
expect(last_response.status).to eq(200)
end

context 'regexp with array' do
it 'refuses inapppopriate items' do
get '/regexp_with_array', names: ['invalid name', 'abc']
expect(last_response.status).to eq(400)
expect(last_response.body).to eq('{"error":"names is invalid"}')
end

it 'refuses empty items' do
get '/regexp_with_array', names: ['', 'abc']
expect(last_response.status).to eq(400)
expect(last_response.body).to eq('{"error":"names is invalid"}')
end

it 'refuses nil items' do
get '/regexp_with_array', names: [nil, 'abc']
expect(last_response.status).to eq(400)
expect(last_response.body).to eq('{"error":"names is invalid"}')
end

it 'accepts valid items' do
get '/regexp_with_array', names: ['bob']
expect(last_response.status).to eq(200)
end

it 'accepts nil instead of array' do
get '/regexp_with_array', names: nil
expect(last_response.status).to eq(200)
end
end

context 'nested regexp with array' do
it 'refuses inapppopriate' do
get '/nested_regexp_with_array', people: 'invalid name'
expect(last_response.status).to eq(400)
expect(last_response.body).to eq('{"error":"people is invalid, people[names] is missing, people[names] is invalid"}')
end
end
end