Skip to content

Commit 2d34f29

Browse files
akoltundblock
authored andcommitted
Allow to use regexp validator with arrays (#1503)
1 parent cb537aa commit 2d34f29

File tree

3 files changed

+91
-1
lines changed

3 files changed

+91
-1
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ Next Release
22
============
33

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

67
0.18.0 (10/7/2016)
78
==================

lib/grape/validations/validators/regexp.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ 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 unless params.respond_to?(:key?) && params.key?(attr_name)
6+
return if Array.wrap(params[attr_name]).all? { |param| param.nil? || (param.to_s =~ (options_key?(:value) ? @option[:value] : @option)) }
67
raise Grape::Exceptions::Validation, params: [@scope.full_name(attr_name)], message: message(:regexp)
78
end
89
end

spec/grape/validations/validators/regexp_spec.rb

+88
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,33 @@ 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
34+
35+
params do
36+
requires :people, type: Hash do
37+
requires :names, type: Array[String], regexp: /^[a-z]+$/
38+
end
39+
end
40+
get 'nested_regexp_with_array' do
41+
end
2242
end
2343
end
2444
end
@@ -51,6 +71,36 @@ def app
5171
get '/custom_message', name: 'bob'
5272
expect(last_response.status).to eq(200)
5373
end
74+
75+
context 'regexp with array' do
76+
it 'refuses inapppopriate items' do
77+
get '/custom_message/regexp_with_array', names: ['invalid name', 'abc']
78+
expect(last_response.status).to eq(400)
79+
expect(last_response.body).to eq('{"error":"names format is invalid"}')
80+
end
81+
82+
it 'refuses empty items' do
83+
get '/custom_message/regexp_with_array', names: ['', 'abc']
84+
expect(last_response.status).to eq(400)
85+
expect(last_response.body).to eq('{"error":"names format is invalid"}')
86+
end
87+
88+
it 'refuses nil items' do
89+
get '/custom_message/regexp_with_array', names: [nil, 'abc']
90+
expect(last_response.status).to eq(400)
91+
expect(last_response.body).to eq('{"error":"names can\'t be nil"}')
92+
end
93+
94+
it 'accepts valid items' do
95+
get '/custom_message/regexp_with_array', names: ['bob']
96+
expect(last_response.status).to eq(200)
97+
end
98+
99+
it 'accepts nil instead of array' do
100+
get '/custom_message/regexp_with_array', names: nil
101+
expect(last_response.status).to eq(200)
102+
end
103+
end
54104
end
55105

56106
context 'invalid input' do
@@ -76,4 +126,42 @@ def app
76126
get '/', name: 'bob'
77127
expect(last_response.status).to eq(200)
78128
end
129+
130+
context 'regexp with array' do
131+
it 'refuses inapppopriate items' do
132+
get '/regexp_with_array', names: ['invalid name', 'abc']
133+
expect(last_response.status).to eq(400)
134+
expect(last_response.body).to eq('{"error":"names is invalid"}')
135+
end
136+
137+
it 'refuses empty items' do
138+
get '/regexp_with_array', names: ['', 'abc']
139+
expect(last_response.status).to eq(400)
140+
expect(last_response.body).to eq('{"error":"names is invalid"}')
141+
end
142+
143+
it 'refuses nil items' do
144+
get '/regexp_with_array', names: [nil, 'abc']
145+
expect(last_response.status).to eq(400)
146+
expect(last_response.body).to eq('{"error":"names is invalid"}')
147+
end
148+
149+
it 'accepts valid items' do
150+
get '/regexp_with_array', names: ['bob']
151+
expect(last_response.status).to eq(200)
152+
end
153+
154+
it 'accepts nil instead of array' do
155+
get '/regexp_with_array', names: nil
156+
expect(last_response.status).to eq(200)
157+
end
158+
end
159+
160+
context 'nested regexp with array' do
161+
it 'refuses inapppopriate' do
162+
get '/nested_regexp_with_array', people: 'invalid name'
163+
expect(last_response.status).to eq(400)
164+
expect(last_response.body).to eq('{"error":"people is invalid, people[names] is missing, people[names] is invalid"}')
165+
end
166+
end
79167
end

0 commit comments

Comments
 (0)