grape 1.6.0 → 1.6.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (104) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +26 -0
  3. data/CONTRIBUTING.md +2 -1
  4. data/README.md +9 -1
  5. data/UPGRADING.md +4 -4
  6. data/lib/grape/api.rb +12 -0
  7. data/lib/grape/dsl/headers.rb +5 -2
  8. data/lib/grape/dsl/helpers.rb +1 -1
  9. data/lib/grape/middleware/auth/dsl.rb +7 -1
  10. data/lib/grape/middleware/base.rb +1 -1
  11. data/lib/grape/util/strict_hash_configuration.rb +1 -1
  12. data/lib/grape/validations/validators/all_or_none.rb +7 -5
  13. data/lib/grape/validations/validators/allow_blank.rb +9 -7
  14. data/lib/grape/validations/validators/as.rb +7 -5
  15. data/lib/grape/validations/validators/at_least_one_of.rb +6 -4
  16. data/lib/grape/validations/validators/base.rb +73 -71
  17. data/lib/grape/validations/validators/coerce.rb +63 -75
  18. data/lib/grape/validations/validators/default.rb +36 -34
  19. data/lib/grape/validations/validators/exactly_one_of.rb +8 -6
  20. data/lib/grape/validations/validators/except_values.rb +13 -11
  21. data/lib/grape/validations/validators/multiple_params_base.rb +24 -22
  22. data/lib/grape/validations/validators/mutual_exclusion.rb +7 -5
  23. data/lib/grape/validations/validators/presence.rb +6 -4
  24. data/lib/grape/validations/validators/regexp.rb +7 -5
  25. data/lib/grape/validations/validators/same_as.rb +17 -15
  26. data/lib/grape/validations/validators/values.rb +59 -57
  27. data/lib/grape/validations.rb +6 -0
  28. data/lib/grape/version.rb +1 -1
  29. data/lib/grape.rb +2 -0
  30. data/spec/grape/api/custom_validations_spec.rb +77 -46
  31. data/spec/grape/api/deeply_included_options_spec.rb +3 -3
  32. data/spec/grape/api/defines_boolean_in_params_spec.rb +2 -1
  33. data/spec/grape/api/invalid_format_spec.rb +2 -0
  34. data/spec/grape/api/recognize_path_spec.rb +1 -1
  35. data/spec/grape/api/shared_helpers_exactly_one_of_spec.rb +9 -15
  36. data/spec/grape/api_remount_spec.rb +16 -15
  37. data/spec/grape/api_spec.rb +317 -193
  38. data/spec/grape/dsl/callbacks_spec.rb +1 -0
  39. data/spec/grape/dsl/headers_spec.rb +39 -9
  40. data/spec/grape/dsl/helpers_spec.rb +3 -2
  41. data/spec/grape/dsl/inside_route_spec.rb +6 -4
  42. data/spec/grape/dsl/logger_spec.rb +16 -18
  43. data/spec/grape/dsl/middleware_spec.rb +1 -0
  44. data/spec/grape/dsl/parameters_spec.rb +1 -0
  45. data/spec/grape/dsl/request_response_spec.rb +1 -0
  46. data/spec/grape/dsl/routing_spec.rb +9 -6
  47. data/spec/grape/endpoint/declared_spec.rb +12 -12
  48. data/spec/grape/endpoint_spec.rb +59 -50
  49. data/spec/grape/entity_spec.rb +13 -13
  50. data/spec/grape/exceptions/body_parse_errors_spec.rb +3 -0
  51. data/spec/grape/exceptions/invalid_accept_header_spec.rb +61 -22
  52. data/spec/grape/exceptions/validation_errors_spec.rb +13 -10
  53. data/spec/grape/exceptions/validation_spec.rb +5 -3
  54. data/spec/grape/extensions/param_builders/hash_spec.rb +7 -7
  55. data/spec/grape/extensions/param_builders/hash_with_indifferent_access_spec.rb +8 -8
  56. data/spec/grape/extensions/param_builders/hashie/mash_spec.rb +8 -8
  57. data/spec/grape/integration/rack_sendfile_spec.rb +1 -1
  58. data/spec/grape/loading_spec.rb +8 -8
  59. data/spec/grape/middleware/auth/dsl_spec.rb +14 -5
  60. data/spec/grape/middleware/auth/strategies_spec.rb +60 -20
  61. data/spec/grape/middleware/base_spec.rb +24 -15
  62. data/spec/grape/middleware/error_spec.rb +1 -0
  63. data/spec/grape/middleware/exception_spec.rb +111 -161
  64. data/spec/grape/middleware/formatter_spec.rb +25 -4
  65. data/spec/grape/middleware/globals_spec.rb +7 -4
  66. data/spec/grape/middleware/stack_spec.rb +11 -11
  67. data/spec/grape/middleware/versioner/accept_version_header_spec.rb +2 -1
  68. data/spec/grape/middleware/versioner/header_spec.rb +14 -13
  69. data/spec/grape/middleware/versioner/param_spec.rb +7 -1
  70. data/spec/grape/middleware/versioner/path_spec.rb +5 -1
  71. data/spec/grape/middleware/versioner_spec.rb +1 -1
  72. data/spec/grape/parser_spec.rb +4 -0
  73. data/spec/grape/path_spec.rb +52 -52
  74. data/spec/grape/presenters/presenter_spec.rb +7 -6
  75. data/spec/grape/request_spec.rb +6 -4
  76. data/spec/grape/util/inheritable_setting_spec.rb +7 -7
  77. data/spec/grape/util/inheritable_values_spec.rb +3 -2
  78. data/spec/grape/util/reverse_stackable_values_spec.rb +3 -1
  79. data/spec/grape/util/stackable_values_spec.rb +7 -5
  80. data/spec/grape/validations/instance_behaivour_spec.rb +9 -10
  81. data/spec/grape/validations/multiple_attributes_iterator_spec.rb +1 -0
  82. data/spec/grape/validations/params_scope_spec.rb +9 -7
  83. data/spec/grape/validations/single_attribute_iterator_spec.rb +1 -0
  84. data/spec/grape/validations/types/primitive_coercer_spec.rb +2 -2
  85. data/spec/grape/validations/types_spec.rb +8 -8
  86. data/spec/grape/validations/validators/all_or_none_spec.rb +50 -56
  87. data/spec/grape/validations/validators/allow_blank_spec.rb +136 -140
  88. data/spec/grape/validations/validators/at_least_one_of_spec.rb +50 -56
  89. data/spec/grape/validations/validators/coerce_spec.rb +10 -12
  90. data/spec/grape/validations/validators/default_spec.rb +72 -78
  91. data/spec/grape/validations/validators/exactly_one_of_spec.rb +71 -77
  92. data/spec/grape/validations/validators/except_values_spec.rb +1 -1
  93. data/spec/grape/validations/validators/mutual_exclusion_spec.rb +71 -77
  94. data/spec/grape/validations/validators/presence_spec.rb +16 -1
  95. data/spec/grape/validations/validators/regexp_spec.rb +25 -31
  96. data/spec/grape/validations/validators/same_as_spec.rb +14 -20
  97. data/spec/grape/validations/validators/values_spec.rb +172 -171
  98. data/spec/grape/validations_spec.rb +45 -16
  99. data/spec/integration/eager_load/eager_load_spec.rb +2 -2
  100. data/spec/integration/multi_json/json_spec.rb +1 -1
  101. data/spec/integration/multi_xml/xml_spec.rb +1 -1
  102. data/spec/shared/versioning_examples.rb +10 -7
  103. data/spec/spec_helper.rb +11 -1
  104. metadata +102 -102
@@ -2,73 +2,67 @@
2
2
 
3
3
  require 'spec_helper'
4
4
 
5
- describe Grape::Validations::AllOrNoneOfValidator do
6
- describe '#validate!' do
7
- subject(:validate) { post path, params }
8
-
9
- module ValidationsSpec
10
- module AllOrNoneOfValidatorSpec
11
- class API < Grape::API
12
- rescue_from Grape::Exceptions::ValidationErrors do |e|
13
- error!(e.errors.transform_keys! { |key| key.join(',') }, 400)
14
- end
5
+ describe Grape::Validations::Validators::AllOrNoneOfValidator do
6
+ let_it_be(:app) do
7
+ Class.new(Grape::API) do
8
+ rescue_from Grape::Exceptions::ValidationErrors do |e|
9
+ error!(e.errors.transform_keys! { |key| key.join(',') }, 400)
10
+ end
15
11
 
16
- params do
17
- optional :beer, :wine, type: Boolean
18
- all_or_none_of :beer, :wine
19
- end
20
- post do
21
- end
12
+ params do
13
+ optional :beer, :wine, type: Grape::API::Boolean
14
+ all_or_none_of :beer, :wine
15
+ end
16
+ post do
17
+ end
22
18
 
23
- params do
24
- optional :beer, :wine, :other, type: Boolean
25
- all_or_none_of :beer, :wine
26
- end
27
- post 'mixed-params' do
28
- end
19
+ params do
20
+ optional :beer, :wine, :other, type: Grape::API::Boolean
21
+ all_or_none_of :beer, :wine
22
+ end
23
+ post 'mixed-params' do
24
+ end
29
25
 
30
- params do
31
- optional :beer, :wine, type: Boolean
32
- all_or_none_of :beer, :wine, message: 'choose all or none'
33
- end
34
- post '/custom-message' do
35
- end
26
+ params do
27
+ optional :beer, :wine, type: Grape::API::Boolean
28
+ all_or_none_of :beer, :wine, message: 'choose all or none'
29
+ end
30
+ post '/custom-message' do
31
+ end
36
32
 
37
- params do
38
- requires :item, type: Hash do
39
- optional :beer, :wine, type: Boolean
40
- all_or_none_of :beer, :wine
41
- end
42
- end
43
- post '/nested-hash' do
44
- end
33
+ params do
34
+ requires :item, type: Hash do
35
+ optional :beer, :wine, type: Grape::API::Boolean
36
+ all_or_none_of :beer, :wine
37
+ end
38
+ end
39
+ post '/nested-hash' do
40
+ end
45
41
 
46
- params do
47
- requires :items, type: Array do
48
- optional :beer, :wine, type: Boolean
49
- all_or_none_of :beer, :wine
50
- end
51
- end
52
- post '/nested-array' do
53
- end
42
+ params do
43
+ requires :items, type: Array do
44
+ optional :beer, :wine, type: Grape::API::Boolean
45
+ all_or_none_of :beer, :wine
46
+ end
47
+ end
48
+ post '/nested-array' do
49
+ end
54
50
 
55
- params do
56
- requires :items, type: Array do
57
- requires :nested_items, type: Array do
58
- optional :beer, :wine, type: Boolean
59
- all_or_none_of :beer, :wine
60
- end
61
- end
62
- end
63
- post '/deeply-nested-array' do
51
+ params do
52
+ requires :items, type: Array do
53
+ requires :nested_items, type: Array do
54
+ optional :beer, :wine, type: Grape::API::Boolean
55
+ all_or_none_of :beer, :wine
64
56
  end
65
57
  end
66
58
  end
59
+ post '/deeply-nested-array' do
60
+ end
67
61
  end
62
+ end
68
63
 
69
- def app
70
- ValidationsSpec::AllOrNoneOfValidatorSpec::API
71
- end
64
+ describe '#validate!' do
65
+ subject(:validate) { post path, params }
72
66
 
73
67
  context 'when all restricted params are present' do
74
68
  let(:path) { '/' }
@@ -2,24 +2,139 @@
2
2
 
3
3
  require 'spec_helper'
4
4
 
5
- describe Grape::Validations::AllowBlankValidator do
6
- module ValidationsSpec
7
- module AllowBlankValidatorSpec
8
- class API < Grape::API
9
- default_format :json
5
+ describe Grape::Validations::Validators::AllowBlankValidator do
6
+ let_it_be(:app) do
7
+ Class.new(Grape::API) do
8
+ default_format :json
10
9
 
11
- params do
10
+ params do
11
+ requires :name, allow_blank: false
12
+ end
13
+ get '/disallow_blank'
14
+
15
+ params do
16
+ optional :name, type: String, allow_blank: false
17
+ end
18
+ get '/opt_disallow_string_blank'
19
+
20
+ params do
21
+ optional :name, allow_blank: false
22
+ end
23
+ get '/disallow_blank_optional_param'
24
+
25
+ params do
26
+ requires :name, allow_blank: true
27
+ end
28
+ get '/allow_blank'
29
+
30
+ params do
31
+ requires :val, type: DateTime, allow_blank: true
32
+ end
33
+ get '/allow_datetime_blank'
34
+
35
+ params do
36
+ requires :val, type: DateTime, allow_blank: false
37
+ end
38
+ get '/disallow_datetime_blank'
39
+
40
+ params do
41
+ requires :val, type: DateTime
42
+ end
43
+ get '/default_allow_datetime_blank'
44
+
45
+ params do
46
+ requires :val, type: Date, allow_blank: true
47
+ end
48
+ get '/allow_date_blank'
49
+
50
+ params do
51
+ requires :val, type: Integer, allow_blank: true
52
+ end
53
+ get '/allow_integer_blank'
54
+
55
+ params do
56
+ requires :val, type: Float, allow_blank: true
57
+ end
58
+ get '/allow_float_blank'
59
+
60
+ params do
61
+ requires :val, type: Integer, allow_blank: true
62
+ end
63
+ get '/allow_integer_blank'
64
+
65
+ params do
66
+ requires :val, type: Symbol, allow_blank: true
67
+ end
68
+ get '/allow_symbol_blank'
69
+
70
+ params do
71
+ requires :val, type: Grape::API::Boolean, allow_blank: true
72
+ end
73
+ get '/allow_boolean_blank'
74
+
75
+ params do
76
+ requires :val, type: Grape::API::Boolean, allow_blank: false
77
+ end
78
+ get '/disallow_boolean_blank'
79
+
80
+ params do
81
+ optional :user, type: Hash do
82
+ requires :name, allow_blank: false
83
+ end
84
+ end
85
+ get '/disallow_blank_required_param_in_an_optional_group'
86
+
87
+ params do
88
+ optional :user, type: Hash do
89
+ requires :name, type: Date, allow_blank: true
90
+ end
91
+ end
92
+ get '/allow_blank_date_param_in_an_optional_group'
93
+
94
+ params do
95
+ optional :user, type: Hash do
96
+ optional :name, allow_blank: false
97
+ requires :age
98
+ end
99
+ end
100
+ get '/disallow_blank_optional_param_in_an_optional_group'
101
+
102
+ params do
103
+ requires :user, type: Hash do
12
104
  requires :name, allow_blank: false
13
105
  end
14
- get '/disallow_blank'
106
+ end
107
+ get '/disallow_blank_required_param_in_a_required_group'
108
+
109
+ params do
110
+ requires :user, type: Hash do
111
+ requires :name, allow_blank: false
112
+ end
113
+ end
114
+ get '/disallow_string_value_in_a_required_hash_group'
115
+
116
+ params do
117
+ requires :user, type: Hash do
118
+ optional :name, allow_blank: false
119
+ end
120
+ end
121
+ get '/disallow_blank_optional_param_in_a_required_group'
122
+
123
+ params do
124
+ optional :user, type: Hash do
125
+ optional :name, allow_blank: false
126
+ end
127
+ end
128
+ get '/disallow_string_value_in_an_optional_hash_group'
15
129
 
130
+ resources :custom_message do
16
131
  params do
17
- optional :name, type: String, allow_blank: false
132
+ requires :name, allow_blank: { value: false, message: 'has no value' }
18
133
  end
19
- get '/opt_disallow_string_blank'
134
+ get
20
135
 
21
136
  params do
22
- optional :name, allow_blank: false
137
+ optional :name, allow_blank: { value: false, message: 'has no value' }
23
138
  end
24
139
  get '/disallow_blank_optional_param'
25
140
 
@@ -34,7 +149,7 @@ describe Grape::Validations::AllowBlankValidator do
34
149
  get '/allow_datetime_blank'
35
150
 
36
151
  params do
37
- requires :val, type: DateTime, allow_blank: false
152
+ requires :val, type: DateTime, allow_blank: { value: false, message: 'has no value' }
38
153
  end
39
154
  get '/disallow_datetime_blank'
40
155
 
@@ -69,18 +184,18 @@ describe Grape::Validations::AllowBlankValidator do
69
184
  get '/allow_symbol_blank'
70
185
 
71
186
  params do
72
- requires :val, type: Boolean, allow_blank: true
187
+ requires :val, type: Grape::API::Boolean, allow_blank: true
73
188
  end
74
189
  get '/allow_boolean_blank'
75
190
 
76
191
  params do
77
- requires :val, type: Boolean, allow_blank: false
192
+ requires :val, type: Grape::API::Boolean, allow_blank: { value: false, message: 'has no value' }
78
193
  end
79
194
  get '/disallow_boolean_blank'
80
195
 
81
196
  params do
82
197
  optional :user, type: Hash do
83
- requires :name, allow_blank: false
198
+ requires :name, allow_blank: { value: false, message: 'has no value' }
84
199
  end
85
200
  end
86
201
  get '/disallow_blank_required_param_in_an_optional_group'
@@ -94,7 +209,7 @@ describe Grape::Validations::AllowBlankValidator do
94
209
 
95
210
  params do
96
211
  optional :user, type: Hash do
97
- optional :name, allow_blank: false
212
+ optional :name, allow_blank: { value: false, message: 'has no value' }
98
213
  requires :age
99
214
  end
100
215
  end
@@ -102,156 +217,35 @@ describe Grape::Validations::AllowBlankValidator do
102
217
 
103
218
  params do
104
219
  requires :user, type: Hash do
105
- requires :name, allow_blank: false
220
+ requires :name, allow_blank: { value: false, message: 'has no value' }
106
221
  end
107
222
  end
108
223
  get '/disallow_blank_required_param_in_a_required_group'
109
224
 
110
225
  params do
111
226
  requires :user, type: Hash do
112
- requires :name, allow_blank: false
227
+ requires :name, allow_blank: { value: false, message: 'has no value' }
113
228
  end
114
229
  end
115
230
  get '/disallow_string_value_in_a_required_hash_group'
116
231
 
117
232
  params do
118
233
  requires :user, type: Hash do
119
- optional :name, allow_blank: false
234
+ optional :name, allow_blank: { value: false, message: 'has no value' }
120
235
  end
121
236
  end
122
237
  get '/disallow_blank_optional_param_in_a_required_group'
123
238
 
124
239
  params do
125
240
  optional :user, type: Hash do
126
- optional :name, allow_blank: false
127
- end
128
- end
129
- get '/disallow_string_value_in_an_optional_hash_group'
130
-
131
- resources :custom_message do
132
- params do
133
- requires :name, allow_blank: { value: false, message: 'has no value' }
134
- end
135
- get
136
-
137
- params do
138
241
  optional :name, allow_blank: { value: false, message: 'has no value' }
139
242
  end
140
- get '/disallow_blank_optional_param'
141
-
142
- params do
143
- requires :name, allow_blank: true
144
- end
145
- get '/allow_blank'
146
-
147
- params do
148
- requires :val, type: DateTime, allow_blank: true
149
- end
150
- get '/allow_datetime_blank'
151
-
152
- params do
153
- requires :val, type: DateTime, allow_blank: { value: false, message: 'has no value' }
154
- end
155
- get '/disallow_datetime_blank'
156
-
157
- params do
158
- requires :val, type: DateTime
159
- end
160
- get '/default_allow_datetime_blank'
161
-
162
- params do
163
- requires :val, type: Date, allow_blank: true
164
- end
165
- get '/allow_date_blank'
166
-
167
- params do
168
- requires :val, type: Integer, allow_blank: true
169
- end
170
- get '/allow_integer_blank'
171
-
172
- params do
173
- requires :val, type: Float, allow_blank: true
174
- end
175
- get '/allow_float_blank'
176
-
177
- params do
178
- requires :val, type: Integer, allow_blank: true
179
- end
180
- get '/allow_integer_blank'
181
-
182
- params do
183
- requires :val, type: Symbol, allow_blank: true
184
- end
185
- get '/allow_symbol_blank'
186
-
187
- params do
188
- requires :val, type: Boolean, allow_blank: true
189
- end
190
- get '/allow_boolean_blank'
191
-
192
- params do
193
- requires :val, type: Boolean, allow_blank: { value: false, message: 'has no value' }
194
- end
195
- get '/disallow_boolean_blank'
196
-
197
- params do
198
- optional :user, type: Hash do
199
- requires :name, allow_blank: { value: false, message: 'has no value' }
200
- end
201
- end
202
- get '/disallow_blank_required_param_in_an_optional_group'
203
-
204
- params do
205
- optional :user, type: Hash do
206
- requires :name, type: Date, allow_blank: true
207
- end
208
- end
209
- get '/allow_blank_date_param_in_an_optional_group'
210
-
211
- params do
212
- optional :user, type: Hash do
213
- optional :name, allow_blank: { value: false, message: 'has no value' }
214
- requires :age
215
- end
216
- end
217
- get '/disallow_blank_optional_param_in_an_optional_group'
218
-
219
- params do
220
- requires :user, type: Hash do
221
- requires :name, allow_blank: { value: false, message: 'has no value' }
222
- end
223
- end
224
- get '/disallow_blank_required_param_in_a_required_group'
225
-
226
- params do
227
- requires :user, type: Hash do
228
- requires :name, allow_blank: { value: false, message: 'has no value' }
229
- end
230
- end
231
- get '/disallow_string_value_in_a_required_hash_group'
232
-
233
- params do
234
- requires :user, type: Hash do
235
- optional :name, allow_blank: { value: false, message: 'has no value' }
236
- end
237
- end
238
- get '/disallow_blank_optional_param_in_a_required_group'
239
-
240
- params do
241
- optional :user, type: Hash do
242
- optional :name, allow_blank: { value: false, message: 'has no value' }
243
- end
244
- end
245
- get '/disallow_string_value_in_an_optional_hash_group'
246
243
  end
244
+ get '/disallow_string_value_in_an_optional_hash_group'
247
245
  end
248
246
  end
249
247
  end
250
248
 
251
- def app
252
- ValidationsSpec::AllowBlankValidatorSpec::API
253
- end
254
-
255
249
  context 'invalid input' do
256
250
  it 'refuses empty string' do
257
251
  get '/disallow_blank', name: ''
@@ -289,10 +283,12 @@ describe Grape::Validations::AllowBlankValidator do
289
283
  get '/custom_message', name: ''
290
284
  expect(last_response.body).to eq('{"error":"name has no value"}')
291
285
  end
286
+
292
287
  it 'refuses empty string for an optional param' do
293
288
  get '/custom_message/disallow_blank_optional_param', name: ''
294
289
  expect(last_response.body).to eq('{"error":"name has no value"}')
295
290
  end
291
+
296
292
  it 'refuses only whitespaces' do
297
293
  get '/custom_message', name: ' '
298
294
  expect(last_response.body).to eq('{"error":"name has no value"}')
@@ -2,73 +2,67 @@
2
2
 
3
3
  require 'spec_helper'
4
4
 
5
- describe Grape::Validations::AtLeastOneOfValidator do
6
- describe '#validate!' do
7
- subject(:validate) { post path, params }
8
-
9
- module ValidationsSpec
10
- module AtLeastOneOfValidatorSpec
11
- class API < Grape::API
12
- rescue_from Grape::Exceptions::ValidationErrors do |e|
13
- error!(e.errors.transform_keys! { |key| key.join(',') }, 400)
14
- end
5
+ describe Grape::Validations::Validators::AtLeastOneOfValidator do
6
+ let_it_be(:app) do
7
+ Class.new(Grape::API) do
8
+ rescue_from Grape::Exceptions::ValidationErrors do |e|
9
+ error!(e.errors.transform_keys! { |key| key.join(',') }, 400)
10
+ end
15
11
 
16
- params do
17
- optional :beer, :wine, :grapefruit
18
- at_least_one_of :beer, :wine, :grapefruit
19
- end
20
- post do
21
- end
12
+ params do
13
+ optional :beer, :wine, :grapefruit
14
+ at_least_one_of :beer, :wine, :grapefruit
15
+ end
16
+ post do
17
+ end
22
18
 
23
- params do
24
- optional :beer, :wine, :grapefruit, :other
25
- at_least_one_of :beer, :wine, :grapefruit
26
- end
27
- post 'mixed-params' do
28
- end
19
+ params do
20
+ optional :beer, :wine, :grapefruit, :other
21
+ at_least_one_of :beer, :wine, :grapefruit
22
+ end
23
+ post 'mixed-params' do
24
+ end
29
25
 
30
- params do
31
- optional :beer, :wine, :grapefruit
32
- at_least_one_of :beer, :wine, :grapefruit, message: 'you should choose something'
33
- end
34
- post '/custom-message' do
35
- end
26
+ params do
27
+ optional :beer, :wine, :grapefruit
28
+ at_least_one_of :beer, :wine, :grapefruit, message: 'you should choose something'
29
+ end
30
+ post '/custom-message' do
31
+ end
36
32
 
37
- params do
38
- requires :item, type: Hash do
39
- optional :beer, :wine, :grapefruit
40
- at_least_one_of :beer, :wine, :grapefruit, message: 'fail'
41
- end
42
- end
43
- post '/nested-hash' do
44
- end
33
+ params do
34
+ requires :item, type: Hash do
35
+ optional :beer, :wine, :grapefruit
36
+ at_least_one_of :beer, :wine, :grapefruit, message: 'fail'
37
+ end
38
+ end
39
+ post '/nested-hash' do
40
+ end
45
41
 
46
- params do
47
- requires :items, type: Array do
48
- optional :beer, :wine, :grapefruit
49
- at_least_one_of :beer, :wine, :grapefruit, message: 'fail'
50
- end
51
- end
52
- post '/nested-array' do
53
- end
42
+ params do
43
+ requires :items, type: Array do
44
+ optional :beer, :wine, :grapefruit
45
+ at_least_one_of :beer, :wine, :grapefruit, message: 'fail'
46
+ end
47
+ end
48
+ post '/nested-array' do
49
+ end
54
50
 
55
- params do
56
- requires :items, type: Array do
57
- requires :nested_items, type: Array do
58
- optional :beer, :wine, :grapefruit
59
- at_least_one_of :beer, :wine, :grapefruit, message: 'fail'
60
- end
61
- end
62
- end
63
- post '/deeply-nested-array' do
51
+ params do
52
+ requires :items, type: Array do
53
+ requires :nested_items, type: Array do
54
+ optional :beer, :wine, :grapefruit
55
+ at_least_one_of :beer, :wine, :grapefruit, message: 'fail'
64
56
  end
65
57
  end
66
58
  end
59
+ post '/deeply-nested-array' do
60
+ end
67
61
  end
62
+ end
68
63
 
69
- def app
70
- ValidationsSpec::AtLeastOneOfValidatorSpec::API
71
- end
64
+ describe '#validate!' do
65
+ subject(:validate) { post path, params }
72
66
 
73
67
  context 'when all restricted params are present' do
74
68
  let(:path) { '/' }
@@ -2,7 +2,7 @@
2
2
 
3
3
  require 'spec_helper'
4
4
 
5
- describe Grape::Validations::CoerceValidator do
5
+ describe Grape::Validations::Validators::CoerceValidator do
6
6
  subject do
7
7
  Class.new(Grape::API)
8
8
  end
@@ -23,7 +23,7 @@ describe Grape::Validations::CoerceValidator do
23
23
  end
24
24
 
25
25
  context 'i18n' do
26
- after :each do
26
+ after do
27
27
  I18n.available_locales = %i[en]
28
28
  I18n.locale = :en
29
29
  I18n.default_locale = :en
@@ -83,7 +83,7 @@ describe Grape::Validations::CoerceValidator do
83
83
  context 'on custom coercion rules' do
84
84
  before do
85
85
  subject.params do
86
- requires :a, types: { value: [Boolean, String], message: 'type cast is invalid' }, coerce_with: (lambda do |val|
86
+ requires :a, types: { value: [Grape::API::Boolean, String], message: 'type cast is invalid' }, coerce_with: (lambda do |val|
87
87
  case val
88
88
  when 'yup'
89
89
  true
@@ -171,9 +171,9 @@ describe Grape::Validations::CoerceValidator do
171
171
  expect(last_response.body).to eq('BigDecimal 45.1')
172
172
  end
173
173
 
174
- it 'Boolean' do
174
+ it 'Grape::API::Boolean' do
175
175
  subject.params do
176
- requires :boolean, type: Boolean
176
+ requires :boolean, type: Grape::API::Boolean
177
177
  end
178
178
  subject.post '/boolean' do
179
179
  params[:boolean]
@@ -370,9 +370,9 @@ describe Grape::Validations::CoerceValidator do
370
370
  end
371
371
  end
372
372
 
373
- it 'Boolean' do
373
+ it 'Grape::API::Boolean' do
374
374
  subject.params do
375
- requires :boolean, type: Boolean
375
+ requires :boolean, type: Grape::API::Boolean
376
376
  end
377
377
  subject.get '/boolean' do
378
378
  params[:boolean].class
@@ -1018,11 +1018,9 @@ describe Grape::Validations::CoerceValidator do
1018
1018
  end
1019
1019
 
1020
1020
  context 'multiple types' do
1021
- Boolean = Grape::API::Boolean
1022
-
1023
1021
  it 'coerces to first possible type' do
1024
1022
  subject.params do
1025
- requires :a, types: [Boolean, Integer, String]
1023
+ requires :a, types: [Grape::API::Boolean, Integer, String]
1026
1024
  end
1027
1025
  subject.get '/' do
1028
1026
  params[:a].class.to_s
@@ -1043,7 +1041,7 @@ describe Grape::Validations::CoerceValidator do
1043
1041
 
1044
1042
  it 'fails when no coercion is possible' do
1045
1043
  subject.params do
1046
- requires :a, types: [Boolean, Integer]
1044
+ requires :a, types: [Grape::API::Boolean, Integer]
1047
1045
  end
1048
1046
  subject.get '/' do
1049
1047
  params[:a].class.to_s
@@ -1202,7 +1200,7 @@ describe Grape::Validations::CoerceValidator do
1202
1200
  context 'custom coercion rules' do
1203
1201
  before do
1204
1202
  subject.params do
1205
- requires :a, types: [Boolean, String], coerce_with: (lambda do |val|
1203
+ requires :a, types: [Grape::API::Boolean, String], coerce_with: (lambda do |val|
1206
1204
  case val
1207
1205
  when 'yup'
1208
1206
  true