grape 1.8.0 → 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (210) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +65 -1
  3. data/README.md +377 -334
  4. data/UPGRADING.md +231 -6
  5. data/grape.gemspec +6 -10
  6. data/lib/grape/api/instance.rb +13 -10
  7. data/lib/grape/api.rb +17 -8
  8. data/lib/grape/content_types.rb +0 -2
  9. data/lib/grape/cookies.rb +2 -1
  10. data/lib/grape/dry_types.rb +0 -2
  11. data/lib/grape/dsl/desc.rb +23 -21
  12. data/lib/grape/dsl/headers.rb +1 -1
  13. data/lib/grape/dsl/inside_route.rb +47 -22
  14. data/lib/grape/dsl/parameters.rb +4 -3
  15. data/lib/grape/dsl/routing.rb +20 -4
  16. data/lib/grape/dsl/validations.rb +13 -0
  17. data/lib/grape/endpoint.rb +15 -10
  18. data/lib/grape/{util/env.rb → env.rb} +0 -5
  19. data/lib/grape/error_formatter/txt.rb +11 -10
  20. data/lib/grape/exceptions/base.rb +3 -3
  21. data/lib/grape/exceptions/missing_group_type.rb +1 -1
  22. data/lib/grape/exceptions/unsupported_group_type.rb +1 -1
  23. data/lib/grape/exceptions/validation.rb +0 -2
  24. data/lib/grape/exceptions/validation_array_errors.rb +1 -0
  25. data/lib/grape/exceptions/validation_errors.rb +1 -3
  26. data/lib/grape/extensions/hash.rb +5 -1
  27. data/lib/grape/http/headers.rb +18 -24
  28. data/lib/grape/{util/json.rb → json.rb} +1 -3
  29. data/lib/grape/locale/en.yml +3 -0
  30. data/lib/grape/middleware/auth/base.rb +0 -2
  31. data/lib/grape/middleware/auth/dsl.rb +0 -2
  32. data/lib/grape/middleware/auth/strategies.rb +1 -2
  33. data/lib/grape/middleware/base.rb +0 -2
  34. data/lib/grape/middleware/error.rb +55 -50
  35. data/lib/grape/middleware/formatter.rb +21 -18
  36. data/lib/grape/middleware/globals.rb +1 -3
  37. data/lib/grape/middleware/stack.rb +2 -3
  38. data/lib/grape/middleware/versioner/accept_version_header.rb +0 -2
  39. data/lib/grape/middleware/versioner/header.rb +17 -163
  40. data/lib/grape/middleware/versioner/param.rb +2 -4
  41. data/lib/grape/middleware/versioner/path.rb +1 -3
  42. data/lib/grape/namespace.rb +3 -4
  43. data/lib/grape/path.rb +24 -29
  44. data/lib/grape/railtie.rb +9 -0
  45. data/lib/grape/request.rb +3 -5
  46. data/lib/grape/router/base_route.rb +39 -0
  47. data/lib/grape/router/greedy_route.rb +20 -0
  48. data/lib/grape/router/pattern.rb +39 -30
  49. data/lib/grape/router/route.rb +22 -59
  50. data/lib/grape/router.rb +30 -36
  51. data/lib/grape/util/accept/header.rb +19 -0
  52. data/lib/grape/util/accept_header_handler.rb +105 -0
  53. data/lib/grape/util/base_inheritable.rb +4 -4
  54. data/lib/grape/util/cache.rb +0 -3
  55. data/lib/grape/util/endpoint_configuration.rb +1 -1
  56. data/lib/grape/util/header.rb +13 -0
  57. data/lib/grape/util/inheritable_values.rb +0 -2
  58. data/lib/grape/util/lazy/block.rb +29 -0
  59. data/lib/grape/util/lazy/object.rb +45 -0
  60. data/lib/grape/util/lazy/value.rb +38 -0
  61. data/lib/grape/util/lazy/value_array.rb +21 -0
  62. data/lib/grape/util/lazy/value_enumerable.rb +34 -0
  63. data/lib/grape/util/lazy/value_hash.rb +21 -0
  64. data/lib/grape/util/media_type.rb +70 -0
  65. data/lib/grape/util/reverse_stackable_values.rb +1 -6
  66. data/lib/grape/util/stackable_values.rb +1 -6
  67. data/lib/grape/util/strict_hash_configuration.rb +3 -3
  68. data/lib/grape/validations/attributes_doc.rb +38 -36
  69. data/lib/grape/validations/contract_scope.rb +71 -0
  70. data/lib/grape/validations/params_scope.rb +10 -9
  71. data/lib/grape/validations/types/array_coercer.rb +0 -2
  72. data/lib/grape/validations/types/build_coercer.rb +69 -71
  73. data/lib/grape/validations/types/dry_type_coercer.rb +1 -11
  74. data/lib/grape/validations/types/json.rb +0 -2
  75. data/lib/grape/validations/types/primitive_coercer.rb +0 -2
  76. data/lib/grape/validations/types/set_coercer.rb +0 -3
  77. data/lib/grape/validations/types.rb +0 -3
  78. data/lib/grape/validations/validators/base.rb +2 -1
  79. data/lib/grape/validations/validators/default_validator.rb +5 -1
  80. data/lib/grape/validations/validators/length_validator.rb +42 -0
  81. data/lib/grape/validations/validators/values_validator.rb +8 -3
  82. data/lib/grape/validations.rb +3 -7
  83. data/lib/grape/version.rb +1 -1
  84. data/lib/grape/{util/xml.rb → xml.rb} +1 -1
  85. data/lib/grape.rb +38 -269
  86. metadata +33 -274
  87. data/lib/grape/eager_load.rb +0 -20
  88. data/lib/grape/middleware/versioner/parse_media_type_patch.rb +0 -24
  89. data/lib/grape/router/attribute_translator.rb +0 -63
  90. data/lib/grape/util/lazy_block.rb +0 -27
  91. data/lib/grape/util/lazy_object.rb +0 -43
  92. data/lib/grape/util/lazy_value.rb +0 -91
  93. data/spec/grape/api/custom_validations_spec.rb +0 -213
  94. data/spec/grape/api/deeply_included_options_spec.rb +0 -56
  95. data/spec/grape/api/defines_boolean_in_params_spec.rb +0 -38
  96. data/spec/grape/api/documentation_spec.rb +0 -59
  97. data/spec/grape/api/inherited_helpers_spec.rb +0 -114
  98. data/spec/grape/api/instance_spec.rb +0 -103
  99. data/spec/grape/api/invalid_format_spec.rb +0 -45
  100. data/spec/grape/api/namespace_parameters_in_route_spec.rb +0 -38
  101. data/spec/grape/api/nested_helpers_spec.rb +0 -50
  102. data/spec/grape/api/optional_parameters_in_route_spec.rb +0 -43
  103. data/spec/grape/api/parameters_modification_spec.rb +0 -41
  104. data/spec/grape/api/patch_method_helpers_spec.rb +0 -79
  105. data/spec/grape/api/recognize_path_spec.rb +0 -21
  106. data/spec/grape/api/required_parameters_in_route_spec.rb +0 -37
  107. data/spec/grape/api/required_parameters_with_invalid_method_spec.rb +0 -26
  108. data/spec/grape/api/routes_with_requirements_spec.rb +0 -59
  109. data/spec/grape/api/shared_helpers_exactly_one_of_spec.rb +0 -41
  110. data/spec/grape/api/shared_helpers_spec.rb +0 -36
  111. data/spec/grape/api_remount_spec.rb +0 -509
  112. data/spec/grape/api_spec.rb +0 -4356
  113. data/spec/grape/dsl/callbacks_spec.rb +0 -45
  114. data/spec/grape/dsl/desc_spec.rb +0 -98
  115. data/spec/grape/dsl/headers_spec.rb +0 -62
  116. data/spec/grape/dsl/helpers_spec.rb +0 -100
  117. data/spec/grape/dsl/inside_route_spec.rb +0 -531
  118. data/spec/grape/dsl/logger_spec.rb +0 -24
  119. data/spec/grape/dsl/middleware_spec.rb +0 -60
  120. data/spec/grape/dsl/parameters_spec.rb +0 -180
  121. data/spec/grape/dsl/request_response_spec.rb +0 -225
  122. data/spec/grape/dsl/routing_spec.rb +0 -275
  123. data/spec/grape/dsl/settings_spec.rb +0 -261
  124. data/spec/grape/dsl/validations_spec.rb +0 -55
  125. data/spec/grape/endpoint/declared_spec.rb +0 -846
  126. data/spec/grape/endpoint_spec.rb +0 -1085
  127. data/spec/grape/entity_spec.rb +0 -336
  128. data/spec/grape/exceptions/base_spec.rb +0 -81
  129. data/spec/grape/exceptions/body_parse_errors_spec.rb +0 -185
  130. data/spec/grape/exceptions/invalid_accept_header_spec.rb +0 -358
  131. data/spec/grape/exceptions/invalid_formatter_spec.rb +0 -15
  132. data/spec/grape/exceptions/invalid_response_spec.rb +0 -11
  133. data/spec/grape/exceptions/invalid_versioner_option_spec.rb +0 -15
  134. data/spec/grape/exceptions/missing_group_type_spec.rb +0 -17
  135. data/spec/grape/exceptions/missing_mime_type_spec.rb +0 -17
  136. data/spec/grape/exceptions/missing_option_spec.rb +0 -15
  137. data/spec/grape/exceptions/unknown_options_spec.rb +0 -15
  138. data/spec/grape/exceptions/unknown_validator_spec.rb +0 -15
  139. data/spec/grape/exceptions/unsupported_group_type_spec.rb +0 -19
  140. data/spec/grape/exceptions/validation_errors_spec.rb +0 -92
  141. data/spec/grape/exceptions/validation_spec.rb +0 -19
  142. data/spec/grape/extensions/param_builders/hash_spec.rb +0 -83
  143. data/spec/grape/extensions/param_builders/hash_with_indifferent_access_spec.rb +0 -105
  144. data/spec/grape/extensions/param_builders/hashie/mash_spec.rb +0 -79
  145. data/spec/grape/grape_spec.rb +0 -9
  146. data/spec/grape/integration/global_namespace_function_spec.rb +0 -29
  147. data/spec/grape/integration/rack_sendfile_spec.rb +0 -48
  148. data/spec/grape/integration/rack_spec.rb +0 -51
  149. data/spec/grape/loading_spec.rb +0 -44
  150. data/spec/grape/middleware/auth/base_spec.rb +0 -31
  151. data/spec/grape/middleware/auth/dsl_spec.rb +0 -60
  152. data/spec/grape/middleware/auth/strategies_spec.rb +0 -120
  153. data/spec/grape/middleware/base_spec.rb +0 -221
  154. data/spec/grape/middleware/error_spec.rb +0 -85
  155. data/spec/grape/middleware/exception_spec.rb +0 -294
  156. data/spec/grape/middleware/formatter_spec.rb +0 -461
  157. data/spec/grape/middleware/globals_spec.rb +0 -30
  158. data/spec/grape/middleware/stack_spec.rb +0 -155
  159. data/spec/grape/middleware/versioner/accept_version_header_spec.rb +0 -122
  160. data/spec/grape/middleware/versioner/header_spec.rb +0 -345
  161. data/spec/grape/middleware/versioner/param_spec.rb +0 -171
  162. data/spec/grape/middleware/versioner/path_spec.rb +0 -62
  163. data/spec/grape/middleware/versioner_spec.rb +0 -21
  164. data/spec/grape/named_api_spec.rb +0 -19
  165. data/spec/grape/parser_spec.rb +0 -86
  166. data/spec/grape/path_spec.rb +0 -252
  167. data/spec/grape/presenters/presenter_spec.rb +0 -71
  168. data/spec/grape/request_spec.rb +0 -126
  169. data/spec/grape/util/inheritable_setting_spec.rb +0 -242
  170. data/spec/grape/util/inheritable_values_spec.rb +0 -79
  171. data/spec/grape/util/reverse_stackable_values_spec.rb +0 -134
  172. data/spec/grape/util/stackable_values_spec.rb +0 -128
  173. data/spec/grape/util/strict_hash_configuration_spec.rb +0 -38
  174. data/spec/grape/validations/attributes_doc_spec.rb +0 -153
  175. data/spec/grape/validations/instance_behaivour_spec.rb +0 -43
  176. data/spec/grape/validations/multiple_attributes_iterator_spec.rb +0 -38
  177. data/spec/grape/validations/params_scope_spec.rb +0 -1420
  178. data/spec/grape/validations/single_attribute_iterator_spec.rb +0 -56
  179. data/spec/grape/validations/types/array_coercer_spec.rb +0 -33
  180. data/spec/grape/validations/types/primitive_coercer_spec.rb +0 -150
  181. data/spec/grape/validations/types/set_coercer_spec.rb +0 -32
  182. data/spec/grape/validations/types_spec.rb +0 -111
  183. data/spec/grape/validations/validators/all_or_none_spec.rb +0 -162
  184. data/spec/grape/validations/validators/allow_blank_spec.rb +0 -575
  185. data/spec/grape/validations/validators/at_least_one_of_spec.rb +0 -205
  186. data/spec/grape/validations/validators/base_spec.rb +0 -38
  187. data/spec/grape/validations/validators/coerce_spec.rb +0 -1261
  188. data/spec/grape/validations/validators/default_spec.rb +0 -463
  189. data/spec/grape/validations/validators/exactly_one_of_spec.rb +0 -233
  190. data/spec/grape/validations/validators/except_values_spec.rb +0 -192
  191. data/spec/grape/validations/validators/mutual_exclusion_spec.rb +0 -214
  192. data/spec/grape/validations/validators/presence_spec.rb +0 -315
  193. data/spec/grape/validations/validators/regexp_spec.rb +0 -161
  194. data/spec/grape/validations/validators/same_as_spec.rb +0 -57
  195. data/spec/grape/validations/validators/values_spec.rb +0 -733
  196. data/spec/grape/validations/validators/zh-CN.yml +0 -10
  197. data/spec/grape/validations_spec.rb +0 -2030
  198. data/spec/integration/eager_load/eager_load_spec.rb +0 -15
  199. data/spec/integration/multi_json/json_spec.rb +0 -7
  200. data/spec/integration/multi_xml/xml_spec.rb +0 -7
  201. data/spec/shared/deprecated_class_examples.rb +0 -16
  202. data/spec/shared/versioning_examples.rb +0 -215
  203. data/spec/spec_helper.rb +0 -52
  204. data/spec/support/basic_auth_encode_helpers.rb +0 -11
  205. data/spec/support/chunks.rb +0 -14
  206. data/spec/support/content_type_helpers.rb +0 -15
  207. data/spec/support/endpoint_faker.rb +0 -25
  208. data/spec/support/file_streamer.rb +0 -13
  209. data/spec/support/integer_helpers.rb +0 -13
  210. data/spec/support/versioned_helpers.rb +0 -55
@@ -1,315 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- describe Grape::Validations::Validators::PresenceValidator do
4
- subject do
5
- Class.new(Grape::API) do
6
- format :json
7
- end
8
- end
9
-
10
- def app
11
- subject
12
- end
13
-
14
- context 'without validation' do
15
- before do
16
- subject.resource :bacons do
17
- get do
18
- 'All the bacon'
19
- end
20
- end
21
- end
22
-
23
- it 'does not validate for any params' do
24
- get '/bacons'
25
- expect(last_response.status).to eq(200)
26
- expect(last_response.body).to eq('All the bacon'.to_json)
27
- end
28
- end
29
-
30
- context 'with a custom validation message' do
31
- before do
32
- subject.resource :requires do
33
- params do
34
- requires :email, type: String, allow_blank: { value: false, message: 'has no value' }, regexp: { value: /^\S+$/, message: 'format is invalid' }, message: 'is required'
35
- end
36
- get do
37
- 'Hello'
38
- end
39
- end
40
- end
41
-
42
- it 'requires when missing' do
43
- get '/requires'
44
- expect(last_response.status).to eq(400)
45
- expect(last_response.body).to eq('{"error":"email is required, email has no value"}')
46
- end
47
-
48
- it 'requires when empty' do
49
- get '/requires', email: ''
50
- expect(last_response.body).to eq('{"error":"email has no value, email format is invalid"}')
51
- end
52
-
53
- it 'valid when set' do
54
- get '/requires', email: 'bob@example.com'
55
- expect(last_response.status).to eq(200)
56
- expect(last_response.body).to eq('Hello'.to_json)
57
- end
58
- end
59
-
60
- context 'with a required regexp parameter supplied in the POST body' do
61
- before do
62
- subject.format :json
63
- subject.params do
64
- requires :id, regexp: /^[0-9]+$/
65
- end
66
- subject.post do
67
- { ret: params[:id] }
68
- end
69
- end
70
-
71
- it 'validates id' do
72
- post '/'
73
- expect(last_response.status).to eq(400)
74
- expect(last_response.body).to eq('{"error":"id is missing"}')
75
-
76
- io = StringIO.new('{"id" : "a56b"}')
77
- post '/', {}, 'rack.input' => io, 'CONTENT_TYPE' => 'application/json', 'CONTENT_LENGTH' => io.length
78
- expect(last_response.body).to eq('{"error":"id is invalid"}')
79
- expect(last_response.status).to eq(400)
80
-
81
- io = StringIO.new('{"id" : 56}')
82
- post '/', {}, 'rack.input' => io, 'CONTENT_TYPE' => 'application/json', 'CONTENT_LENGTH' => io.length
83
- expect(last_response.body).to eq('{"ret":56}')
84
- expect(last_response.status).to eq(201)
85
- end
86
- end
87
-
88
- context 'with a required non-empty string' do
89
- before do
90
- subject.params do
91
- requires :email, type: String, allow_blank: false, regexp: /^\S+$/
92
- end
93
- subject.get do
94
- 'Hello'
95
- end
96
- end
97
-
98
- it 'requires when missing' do
99
- get '/'
100
- expect(last_response.status).to eq(400)
101
- expect(last_response.body).to eq('{"error":"email is missing, email is empty"}')
102
- end
103
-
104
- it 'requires when empty' do
105
- get '/', email: ''
106
- expect(last_response.status).to eq(400)
107
- expect(last_response.body).to eq('{"error":"email is empty, email is invalid"}')
108
- end
109
-
110
- it 'valid when set' do
111
- get '/', email: 'bob@example.com'
112
- expect(last_response.status).to eq(200)
113
- expect(last_response.body).to eq('Hello'.to_json)
114
- end
115
- end
116
-
117
- context 'with multiple parameters per requires' do
118
- before do
119
- subject.params do
120
- requires :one, :two
121
- end
122
- subject.get '/single-requires' do
123
- 'Hello'
124
- end
125
-
126
- subject.params do
127
- requires :one
128
- requires :two
129
- end
130
- subject.get '/multiple-requires' do
131
- 'Hello'
132
- end
133
- end
134
-
135
- it 'validates for all defined params' do
136
- get '/single-requires'
137
- expect(last_response.status).to eq(400)
138
- single_requires_error = last_response.body
139
-
140
- get '/multiple-requires'
141
- expect(last_response.status).to eq(400)
142
- expect(last_response.body).to eq(single_requires_error)
143
- end
144
- end
145
-
146
- context 'with required parameters and no type' do
147
- before do
148
- subject.params do
149
- requires :name, :company
150
- end
151
- subject.get do
152
- 'Hello'
153
- end
154
- end
155
-
156
- it 'validates name, company' do
157
- get '/'
158
- expect(last_response.status).to eq(400)
159
- expect(last_response.body).to eq('{"error":"name is missing, company is missing"}')
160
-
161
- get '/', name: 'Bob'
162
- expect(last_response.status).to eq(400)
163
- expect(last_response.body).to eq('{"error":"company is missing"}')
164
-
165
- get '/', name: 'Bob', company: 'TestCorp'
166
- expect(last_response.status).to eq(200)
167
- expect(last_response.body).to eq('Hello'.to_json)
168
- end
169
- end
170
-
171
- context 'with nested parameters' do
172
- before do
173
- subject.params do
174
- requires :user, type: Hash do
175
- requires :first_name
176
- requires :last_name
177
- end
178
- end
179
- subject.get '/nested' do
180
- 'Nested'
181
- end
182
- end
183
-
184
- it 'validates nested parameters' do
185
- get '/nested'
186
- expect(last_response.status).to eq(400)
187
- expect(last_response.body).to eq('{"error":"user is missing, user[first_name] is missing, user[last_name] is missing"}')
188
-
189
- get '/nested', user: { first_name: 'Billy' }
190
- expect(last_response.status).to eq(400)
191
- expect(last_response.body).to eq('{"error":"user[last_name] is missing"}')
192
-
193
- get '/nested', user: { first_name: 'Billy', last_name: 'Bob' }
194
- expect(last_response.status).to eq(200)
195
- expect(last_response.body).to eq('Nested'.to_json)
196
- end
197
- end
198
-
199
- context 'with triply nested required parameters' do
200
- before do
201
- subject.params do
202
- requires :admin, type: Hash do
203
- requires :admin_name
204
- requires :super, type: Hash do
205
- requires :user, type: Hash do
206
- requires :first_name
207
- requires :last_name
208
- end
209
- end
210
- end
211
- end
212
- subject.get '/nested_triple' do
213
- 'Nested triple'
214
- end
215
- end
216
-
217
- it 'validates triple nested parameters' do
218
- get '/nested_triple'
219
- expect(last_response.status).to eq(400)
220
- expect(last_response.body).to include '{"error":"admin is missing'
221
-
222
- get '/nested_triple', user: { first_name: 'Billy' }
223
- expect(last_response.status).to eq(400)
224
- expect(last_response.body).to include '{"error":"admin is missing'
225
-
226
- get '/nested_triple', admin: { super: { first_name: 'Billy' } }
227
- expect(last_response.status).to eq(400)
228
- expect(last_response.body).to eq('{"error":"admin[admin_name] is missing, admin[super][user] is missing, admin[super][user][first_name] is missing, admin[super][user][last_name] is missing"}')
229
-
230
- get '/nested_triple', super: { user: { first_name: 'Billy', last_name: 'Bob' } }
231
- expect(last_response.status).to eq(400)
232
- expect(last_response.body).to include '{"error":"admin is missing'
233
-
234
- get '/nested_triple', admin: { super: { user: { first_name: 'Billy' } } }
235
- expect(last_response.status).to eq(400)
236
- expect(last_response.body).to eq('{"error":"admin[admin_name] is missing, admin[super][user][last_name] is missing"}')
237
-
238
- get '/nested_triple', admin: { admin_name: 'admin', super: { user: { first_name: 'Billy' } } }
239
- expect(last_response.status).to eq(400)
240
- expect(last_response.body).to eq('{"error":"admin[super][user][last_name] is missing"}')
241
-
242
- get '/nested_triple', admin: { admin_name: 'admin', super: { user: { first_name: 'Billy', last_name: 'Bob' } } }
243
- expect(last_response.status).to eq(200)
244
- expect(last_response.body).to eq('Nested triple'.to_json)
245
- end
246
- end
247
-
248
- context 'with reused parameter documentation once required and once optional' do
249
- before do
250
- docs = { name: { type: String, desc: 'some name' } }
251
-
252
- subject.params do
253
- requires :all, using: docs
254
- end
255
- subject.get '/required' do
256
- 'Hello required'
257
- end
258
-
259
- subject.params do
260
- optional :all, using: docs
261
- end
262
- subject.get '/optional' do
263
- 'Hello optional'
264
- end
265
- end
266
-
267
- it 'works with required' do
268
- get '/required'
269
- expect(last_response.status).to eq(400)
270
- expect(last_response.body).to eq('{"error":"name is missing"}')
271
-
272
- get '/required', name: 'Bob'
273
- expect(last_response.status).to eq(200)
274
- expect(last_response.body).to eq('Hello required'.to_json)
275
- end
276
-
277
- it 'works with optional' do
278
- get '/optional'
279
- expect(last_response.status).to eq(200)
280
- expect(last_response.body).to eq('Hello optional'.to_json)
281
-
282
- get '/optional', name: 'Bob'
283
- expect(last_response.status).to eq(200)
284
- expect(last_response.body).to eq('Hello optional'.to_json)
285
- end
286
- end
287
-
288
- context 'with a custom type' do
289
- it 'does not validate their type when it is missing' do
290
- class CustomType
291
- def self.parse(value)
292
- return if value.blank?
293
-
294
- new
295
- end
296
- end
297
-
298
- subject.params do
299
- requires :custom, type: CustomType
300
- end
301
- subject.get '/custom' do
302
- 'custom'
303
- end
304
-
305
- get 'custom'
306
-
307
- expect(last_response.status).to eq(400)
308
- expect(last_response.body).to eq('{"error":"custom is missing"}')
309
-
310
- get 'custom', custom: 'filled'
311
-
312
- expect(last_response.status).to eq(200)
313
- end
314
- end
315
- end
@@ -1,161 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- describe Grape::Validations::Validators::RegexpValidator do
4
- let_it_be(:app) do
5
- Class.new(Grape::API) do
6
- default_format :json
7
-
8
- resources :custom_message do
9
- params do
10
- requires :name, regexp: { value: /^[a-z]+$/, message: 'format is invalid' }
11
- end
12
- get do
13
- end
14
-
15
- params do
16
- requires :names, type: { value: Array[String], message: 'can\'t be nil' }, regexp: { value: /^[a-z]+$/, message: 'format is invalid' }
17
- end
18
- get 'regexp_with_array' do
19
- end
20
- end
21
-
22
- params do
23
- requires :name, regexp: /^[a-z]+$/
24
- end
25
- get do
26
- end
27
-
28
- params do
29
- requires :names, type: Array[String], regexp: /^[a-z]+$/
30
- end
31
- get 'regexp_with_array' do
32
- end
33
-
34
- params do
35
- requires :people, type: Hash do
36
- requires :names, type: Array[String], regexp: /^[a-z]+$/
37
- end
38
- end
39
- get 'nested_regexp_with_array' do
40
- end
41
- end
42
- end
43
-
44
- context 'custom validation message' do
45
- context 'with invalid input' do
46
- it 'refuses inapppopriate' do
47
- get '/custom_message', name: 'invalid name'
48
- expect(last_response.status).to eq(400)
49
- expect(last_response.body).to eq('{"error":"name format is invalid"}')
50
- end
51
-
52
- it 'refuses empty' do
53
- get '/custom_message', name: ''
54
- expect(last_response.status).to eq(400)
55
- expect(last_response.body).to eq('{"error":"name format is invalid"}')
56
- end
57
- end
58
-
59
- it 'accepts nil' do
60
- get '/custom_message', name: nil
61
- expect(last_response.status).to eq(200)
62
- end
63
-
64
- it 'accepts valid input' do
65
- get '/custom_message', name: 'bob'
66
- expect(last_response.status).to eq(200)
67
- end
68
-
69
- context 'regexp with array' do
70
- it 'refuses inapppopriate items' do
71
- get '/custom_message/regexp_with_array', names: ['invalid name', 'abc']
72
- expect(last_response.status).to eq(400)
73
- expect(last_response.body).to eq('{"error":"names format is invalid"}')
74
- end
75
-
76
- it 'refuses empty items' do
77
- get '/custom_message/regexp_with_array', names: ['', '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 nil items' do
83
- get '/custom_message/regexp_with_array', names: [nil, 'abc']
84
- expect(last_response.status).to eq(400)
85
- expect(last_response.body).to eq('{"error":"names can\'t be nil"}')
86
- end
87
-
88
- it 'accepts valid items' do
89
- get '/custom_message/regexp_with_array', names: ['bob']
90
- expect(last_response.status).to eq(200)
91
- end
92
-
93
- it 'accepts nil instead of array' do
94
- get '/custom_message/regexp_with_array', names: nil
95
- expect(last_response.status).to eq(200)
96
- end
97
- end
98
- end
99
-
100
- context 'invalid input' do
101
- it 'refuses inapppopriate' do
102
- get '/', name: 'invalid name'
103
- expect(last_response.status).to eq(400)
104
- expect(last_response.body).to eq('{"error":"name is invalid"}')
105
- end
106
-
107
- it 'refuses empty' do
108
- get '/', name: ''
109
- expect(last_response.status).to eq(400)
110
- expect(last_response.body).to eq('{"error":"name is invalid"}')
111
- end
112
- end
113
-
114
- it 'accepts nil' do
115
- get '/', name: nil
116
- expect(last_response.status).to eq(200)
117
- end
118
-
119
- it 'accepts valid input' do
120
- get '/', name: 'bob'
121
- expect(last_response.status).to eq(200)
122
- end
123
-
124
- context 'regexp with array' do
125
- it 'refuses inapppopriate items' do
126
- get '/regexp_with_array', names: ['invalid name', 'abc']
127
- expect(last_response.status).to eq(400)
128
- expect(last_response.body).to eq('{"error":"names is invalid"}')
129
- end
130
-
131
- it 'refuses empty items' do
132
- get '/regexp_with_array', names: ['', '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 nil items' do
138
- get '/regexp_with_array', names: [nil, '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 'accepts valid items' do
144
- get '/regexp_with_array', names: ['bob']
145
- expect(last_response.status).to eq(200)
146
- end
147
-
148
- it 'accepts nil instead of array' do
149
- get '/regexp_with_array', names: nil
150
- expect(last_response.status).to eq(200)
151
- end
152
- end
153
-
154
- context 'nested regexp with array' do
155
- it 'refuses inapppopriate' do
156
- get '/nested_regexp_with_array', people: 'invalid name'
157
- expect(last_response.status).to eq(400)
158
- expect(last_response.body).to eq('{"error":"people is invalid, people[names] is missing, people[names] is invalid"}')
159
- end
160
- end
161
- end
@@ -1,57 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- describe Grape::Validations::Validators::SameAsValidator do
4
- let_it_be(:app) do
5
- Class.new(Grape::API) do
6
- params do
7
- requires :password
8
- requires :password_confirmation, same_as: :password
9
- end
10
- post do
11
- end
12
-
13
- params do
14
- requires :password
15
- requires :password_confirmation, same_as: { value: :password, message: 'not match' }
16
- end
17
- post '/custom-message' do
18
- end
19
- end
20
- end
21
-
22
- describe '/' do
23
- context 'is the same' do
24
- it do
25
- post '/', password: '987654', password_confirmation: '987654'
26
- expect(last_response.status).to eq(201)
27
- expect(last_response.body).to eq('')
28
- end
29
- end
30
-
31
- context 'is not the same' do
32
- it do
33
- post '/', password: '123456', password_confirmation: 'whatever'
34
- expect(last_response.status).to eq(400)
35
- expect(last_response.body).to eq('password_confirmation is not the same as password')
36
- end
37
- end
38
- end
39
-
40
- describe '/custom-message' do
41
- context 'is the same' do
42
- it do
43
- post '/custom-message', password: '987654', password_confirmation: '987654'
44
- expect(last_response.status).to eq(201)
45
- expect(last_response.body).to eq('')
46
- end
47
- end
48
-
49
- context 'is not the same' do
50
- it do
51
- post '/custom-message', password: '123456', password_confirmation: 'whatever'
52
- expect(last_response.status).to eq(400)
53
- expect(last_response.body).to eq('password_confirmation not match')
54
- end
55
- end
56
- end
57
- end