@@ -12,13 +12,33 @@ class API < Grape::API
12
12
end
13
13
get do
14
14
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
15
21
end
16
22
17
23
params do
18
24
requires :name , regexp : /^[a-z]+$/
19
25
end
20
26
get do
21
27
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
22
42
end
23
43
end
24
44
end
@@ -51,6 +71,36 @@ def app
51
71
get '/custom_message' , name : 'bob'
52
72
expect ( last_response . status ) . to eq ( 200 )
53
73
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
54
104
end
55
105
56
106
context 'invalid input' do
@@ -76,4 +126,42 @@ def app
76
126
get '/' , name : 'bob'
77
127
expect ( last_response . status ) . to eq ( 200 )
78
128
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
79
167
end
0 commit comments