Skip to content

Commit 4021ec4

Browse files
Alexander KoltunAlexander Koltun
Alexander Koltun
authored and
Alexander Koltun
committed
Allow to use regexp validator with arrays
1 parent d734415 commit 4021ec4

File tree

3 files changed

+74
-1
lines changed

3 files changed

+74
-1
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
0.17.1 (Next)
22
==================
33

4+
* [#1503](https://github.com/ruby-grape/grape/pull/1503): Allow to use regexp validator with arrays - [@akoltun](https://github.com/akoltun).
45
* [#1480](https://github.com/ruby-grape/grape/pull/1480): Use the ruby-grape-danger gem for PR linting - [@dblock](https://github.com/dblock).
56
* [#1486](https://github.com/ruby-grape/grape/pull/1486): Implemented except in values validator - [@jonmchan](https://github.com/jonmchan).
67
* [#1470](https://github.com/ruby-grape/grape/pull/1470): Drop support for ruby-2.0 - [@namusyaka](https://github.com/namusyaka).

lib/grape/validations/validators/regexp.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ module Grape
22
module Validations
33
class RegexpValidator < Base
44
def validate_param!(attr_name, params)
5-
return unless params.key?(attr_name) && !params[attr_name].nil? && !(params[attr_name].to_s =~ (options_key?(:value) ? @option[:value] : @option))
5+
return if Array.wrap(params[attr_name]).all? { |param| param.nil? || (param.to_s =~ (options_key?(:value) ? @option[:value] : @option)) }
66
raise Grape::Exceptions::Validation, params: [@scope.full_name(attr_name)], message: message(:regexp)
77
end
88
end

spec/grape/validations/validators/regexp_spec.rb

+72
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,25 @@ class API < Grape::API
1212
end
1313
get do
1414
end
15+
16+
params do
17+
requires :names, type: { value: Array[String], message: 'can\'t be nil' }, regexp: { value: /^[a-z]+$/, message: 'format is invalid' }
18+
end
19+
get 'regexp_with_array' do
20+
end
1521
end
1622

1723
params do
1824
requires :name, regexp: /^[a-z]+$/
1925
end
2026
get do
2127
end
28+
29+
params do
30+
requires :names, type: Array[String], regexp: /^[a-z]+$/
31+
end
32+
get 'regexp_with_array' do
33+
end
2234
end
2335
end
2436
end
@@ -51,6 +63,36 @@ def app
5163
get '/custom_message', name: 'bob'
5264
expect(last_response.status).to eq(200)
5365
end
66+
67+
context 'regexp with array' do
68+
it 'refuses inapppopriate items' do
69+
get '/custom_message/regexp_with_array', names: ['invalid name', 'abc']
70+
expect(last_response.status).to eq(400)
71+
expect(last_response.body).to eq('{"error":"names format is invalid"}')
72+
end
73+
74+
it 'refuses empty items' do
75+
get '/custom_message/regexp_with_array', names: ['', 'abc']
76+
expect(last_response.status).to eq(400)
77+
expect(last_response.body).to eq('{"error":"names format is invalid"}')
78+
end
79+
80+
it 'refuses nil items' do
81+
get '/custom_message/regexp_with_array', names: [nil, 'abc']
82+
expect(last_response.status).to eq(400)
83+
expect(last_response.body).to eq('{"error":"names can\'t be nil"}')
84+
end
85+
86+
it 'accepts valid items' do
87+
get '/custom_message/regexp_with_array', names: ['bob']
88+
expect(last_response.status).to eq(200)
89+
end
90+
91+
it 'accepts nil instead of array' do
92+
get '/custom_message/regexp_with_array', names: nil
93+
expect(last_response.status).to eq(200)
94+
end
95+
end
5496
end
5597

5698
context 'invalid input' do
@@ -76,4 +118,34 @@ def app
76118
get '/', name: 'bob'
77119
expect(last_response.status).to eq(200)
78120
end
121+
122+
context 'regexp with array' do
123+
it 'refuses inapppopriate items' do
124+
get '/regexp_with_array', names: ['invalid name', 'abc']
125+
expect(last_response.status).to eq(400)
126+
expect(last_response.body).to eq('{"error":"names is invalid"}')
127+
end
128+
129+
it 'refuses empty items' do
130+
get '/regexp_with_array', names: ['', 'abc']
131+
expect(last_response.status).to eq(400)
132+
expect(last_response.body).to eq('{"error":"names is invalid"}')
133+
end
134+
135+
it 'refuses nil items' do
136+
get '/regexp_with_array', names: [nil, 'abc']
137+
expect(last_response.status).to eq(400)
138+
expect(last_response.body).to eq('{"error":"names is invalid"}')
139+
end
140+
141+
it 'accepts valid items' do
142+
get '/regexp_with_array', names: ['bob']
143+
expect(last_response.status).to eq(200)
144+
end
145+
146+
it 'accepts nil instead of array' do
147+
get '/regexp_with_array', names: nil
148+
expect(last_response.status).to eq(200)
149+
end
150+
end
79151
end

0 commit comments

Comments
 (0)