@@ -12,13 +12,25 @@ 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
22
34
end
23
35
end
24
36
end
@@ -51,6 +63,36 @@ def app
51
63
get '/custom_message' , name : 'bob'
52
64
expect ( last_response . status ) . to eq ( 200 )
53
65
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
54
96
end
55
97
56
98
context 'invalid input' do
@@ -76,4 +118,34 @@ def app
76
118
get '/' , name : 'bob'
77
119
expect ( last_response . status ) . to eq ( 200 )
78
120
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
79
151
end
0 commit comments