grape 1.3.3 → 1.6.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +111 -2
- data/CONTRIBUTING.md +2 -1
- data/README.md +135 -23
- data/UPGRADING.md +237 -46
- data/grape.gemspec +5 -5
- data/lib/grape/api/instance.rb +34 -42
- data/lib/grape/api.rb +21 -16
- data/lib/grape/cookies.rb +2 -0
- data/lib/grape/dsl/callbacks.rb +1 -1
- data/lib/grape/dsl/desc.rb +3 -5
- data/lib/grape/dsl/headers.rb +5 -2
- data/lib/grape/dsl/helpers.rb +8 -5
- data/lib/grape/dsl/inside_route.rb +72 -53
- data/lib/grape/dsl/middleware.rb +4 -4
- data/lib/grape/dsl/parameters.rb +11 -7
- data/lib/grape/dsl/request_response.rb +9 -6
- data/lib/grape/dsl/routing.rb +8 -9
- data/lib/grape/dsl/settings.rb +5 -5
- data/lib/grape/dsl/validations.rb +18 -1
- data/lib/grape/eager_load.rb +1 -1
- data/lib/grape/endpoint.rb +29 -42
- data/lib/grape/error_formatter/json.rb +2 -6
- data/lib/grape/error_formatter/xml.rb +2 -6
- data/lib/grape/exceptions/empty_message_body.rb +11 -0
- data/lib/grape/exceptions/validation.rb +2 -3
- data/lib/grape/exceptions/validation_errors.rb +1 -1
- data/lib/grape/formatter/json.rb +1 -0
- data/lib/grape/formatter/serializable_hash.rb +2 -1
- data/lib/grape/formatter/xml.rb +1 -0
- data/lib/grape/locale/en.yml +1 -1
- data/lib/grape/middleware/auth/base.rb +3 -3
- data/lib/grape/middleware/auth/dsl.rb +7 -1
- data/lib/grape/middleware/base.rb +6 -3
- data/lib/grape/middleware/error.rb +11 -13
- data/lib/grape/middleware/formatter.rb +7 -7
- data/lib/grape/middleware/stack.rb +10 -3
- data/lib/grape/middleware/versioner/accept_version_header.rb +3 -5
- data/lib/grape/middleware/versioner/header.rb +6 -4
- data/lib/grape/middleware/versioner/param.rb +1 -0
- data/lib/grape/middleware/versioner/parse_media_type_patch.rb +2 -1
- data/lib/grape/middleware/versioner/path.rb +2 -0
- data/lib/grape/parser/json.rb +1 -1
- data/lib/grape/parser/xml.rb +1 -1
- data/lib/grape/path.rb +1 -0
- data/lib/grape/request.rb +4 -1
- data/lib/grape/router/attribute_translator.rb +3 -3
- data/lib/grape/router/pattern.rb +1 -1
- data/lib/grape/router/route.rb +2 -2
- data/lib/grape/router.rb +31 -30
- data/lib/grape/{serve_file → serve_stream}/file_body.rb +1 -1
- data/lib/grape/{serve_file → serve_stream}/sendfile_response.rb +1 -1
- data/lib/grape/{serve_file/file_response.rb → serve_stream/stream_response.rb} +8 -8
- data/lib/grape/util/base_inheritable.rb +2 -2
- data/lib/grape/util/inheritable_setting.rb +1 -3
- data/lib/grape/util/lazy_value.rb +4 -2
- data/lib/grape/util/strict_hash_configuration.rb +1 -1
- data/lib/grape/validations/attributes_iterator.rb +8 -0
- data/lib/grape/validations/multiple_attributes_iterator.rb +1 -1
- data/lib/grape/validations/params_scope.rb +97 -62
- data/lib/grape/validations/single_attribute_iterator.rb +1 -1
- data/lib/grape/validations/types/custom_type_coercer.rb +16 -3
- data/lib/grape/validations/types/dry_type_coercer.rb +1 -1
- data/lib/grape/validations/types/invalid_value.rb +24 -0
- data/lib/grape/validations/types/json.rb +2 -1
- data/lib/grape/validations/types/primitive_coercer.rb +4 -5
- data/lib/grape/validations/types.rb +1 -4
- data/lib/grape/validations/validator_factory.rb +1 -1
- data/lib/grape/validations/validators/all_or_none.rb +8 -5
- data/lib/grape/validations/validators/allow_blank.rb +9 -7
- data/lib/grape/validations/validators/as.rb +6 -8
- data/lib/grape/validations/validators/at_least_one_of.rb +7 -4
- data/lib/grape/validations/validators/base.rb +74 -69
- data/lib/grape/validations/validators/coerce.rb +63 -76
- data/lib/grape/validations/validators/default.rb +36 -34
- data/lib/grape/validations/validators/exactly_one_of.rb +9 -6
- data/lib/grape/validations/validators/except_values.rb +13 -11
- data/lib/grape/validations/validators/multiple_params_base.rb +24 -19
- data/lib/grape/validations/validators/mutual_exclusion.rb +8 -5
- data/lib/grape/validations/validators/presence.rb +7 -4
- data/lib/grape/validations/validators/regexp.rb +8 -5
- data/lib/grape/validations/validators/same_as.rb +18 -15
- data/lib/grape/validations/validators/values.rb +61 -56
- data/lib/grape/validations.rb +6 -0
- data/lib/grape/version.rb +1 -1
- data/lib/grape.rb +7 -3
- data/spec/grape/api/custom_validations_spec.rb +77 -45
- data/spec/grape/api/deeply_included_options_spec.rb +3 -3
- data/spec/grape/api/defines_boolean_in_params_spec.rb +2 -1
- data/spec/grape/api/invalid_format_spec.rb +2 -0
- data/spec/grape/api/recognize_path_spec.rb +1 -1
- data/spec/grape/api/routes_with_requirements_spec.rb +8 -8
- data/spec/grape/api/shared_helpers_exactly_one_of_spec.rb +9 -15
- data/spec/grape/api_remount_spec.rb +25 -19
- data/spec/grape/api_spec.rb +576 -211
- data/spec/grape/dsl/callbacks_spec.rb +2 -1
- data/spec/grape/dsl/headers_spec.rb +39 -9
- data/spec/grape/dsl/helpers_spec.rb +3 -2
- data/spec/grape/dsl/inside_route_spec.rb +185 -34
- data/spec/grape/dsl/logger_spec.rb +16 -18
- data/spec/grape/dsl/middleware_spec.rb +2 -1
- data/spec/grape/dsl/parameters_spec.rb +2 -0
- data/spec/grape/dsl/request_response_spec.rb +1 -0
- data/spec/grape/dsl/routing_spec.rb +10 -7
- data/spec/grape/endpoint/declared_spec.rb +848 -0
- data/spec/grape/endpoint_spec.rb +77 -589
- data/spec/grape/entity_spec.rb +29 -23
- data/spec/grape/exceptions/body_parse_errors_spec.rb +3 -0
- data/spec/grape/exceptions/invalid_accept_header_spec.rb +61 -22
- data/spec/grape/exceptions/validation_errors_spec.rb +13 -10
- data/spec/grape/exceptions/validation_spec.rb +5 -3
- data/spec/grape/extensions/param_builders/hash_spec.rb +7 -7
- data/spec/grape/extensions/param_builders/hash_with_indifferent_access_spec.rb +8 -8
- data/spec/grape/extensions/param_builders/hashie/mash_spec.rb +8 -8
- data/spec/grape/integration/rack_sendfile_spec.rb +13 -9
- data/spec/grape/loading_spec.rb +8 -8
- data/spec/grape/middleware/auth/dsl_spec.rb +15 -6
- data/spec/grape/middleware/auth/strategies_spec.rb +61 -21
- data/spec/grape/middleware/base_spec.rb +24 -15
- data/spec/grape/middleware/error_spec.rb +3 -3
- data/spec/grape/middleware/exception_spec.rb +111 -161
- data/spec/grape/middleware/formatter_spec.rb +28 -7
- data/spec/grape/middleware/globals_spec.rb +7 -4
- data/spec/grape/middleware/stack_spec.rb +15 -12
- data/spec/grape/middleware/versioner/accept_version_header_spec.rb +2 -1
- data/spec/grape/middleware/versioner/header_spec.rb +14 -13
- data/spec/grape/middleware/versioner/param_spec.rb +7 -1
- data/spec/grape/middleware/versioner/path_spec.rb +5 -1
- data/spec/grape/middleware/versioner_spec.rb +1 -1
- data/spec/grape/parser_spec.rb +4 -0
- data/spec/grape/path_spec.rb +52 -52
- data/spec/grape/presenters/presenter_spec.rb +7 -6
- data/spec/grape/request_spec.rb +6 -4
- data/spec/grape/util/inheritable_setting_spec.rb +7 -7
- data/spec/grape/util/inheritable_values_spec.rb +3 -2
- data/spec/grape/util/reverse_stackable_values_spec.rb +3 -1
- data/spec/grape/util/stackable_values_spec.rb +7 -5
- data/spec/grape/validations/instance_behaivour_spec.rb +9 -10
- data/spec/grape/validations/multiple_attributes_iterator_spec.rb +14 -3
- data/spec/grape/validations/params_scope_spec.rb +72 -10
- data/spec/grape/validations/single_attribute_iterator_spec.rb +18 -6
- data/spec/grape/validations/types/primitive_coercer_spec.rb +63 -7
- data/spec/grape/validations/types_spec.rb +8 -8
- data/spec/grape/validations/validators/all_or_none_spec.rb +50 -56
- data/spec/grape/validations/validators/allow_blank_spec.rb +136 -140
- data/spec/grape/validations/validators/at_least_one_of_spec.rb +50 -56
- data/spec/grape/validations/validators/coerce_spec.rb +248 -33
- data/spec/grape/validations/validators/default_spec.rb +121 -78
- data/spec/grape/validations/validators/exactly_one_of_spec.rb +71 -77
- data/spec/grape/validations/validators/except_values_spec.rb +4 -3
- data/spec/grape/validations/validators/mutual_exclusion_spec.rb +71 -77
- data/spec/grape/validations/validators/presence_spec.rb +16 -1
- data/spec/grape/validations/validators/regexp_spec.rb +25 -31
- data/spec/grape/validations/validators/same_as_spec.rb +14 -20
- data/spec/grape/validations/validators/values_spec.rb +183 -178
- data/spec/grape/validations_spec.rb +342 -29
- data/spec/integration/eager_load/eager_load_spec.rb +15 -0
- data/spec/integration/multi_json/json_spec.rb +1 -1
- data/spec/integration/multi_xml/xml_spec.rb +1 -1
- data/spec/shared/versioning_examples.rb +32 -29
- data/spec/spec_helper.rb +12 -12
- data/spec/support/basic_auth_encode_helpers.rb +1 -1
- data/spec/support/chunks.rb +14 -0
- data/spec/support/versioned_helpers.rb +4 -6
- metadata +110 -102
@@ -2,11 +2,12 @@
|
|
2
2
|
|
3
3
|
require 'spec_helper'
|
4
4
|
|
5
|
-
describe Grape::Validations::ValuesValidator do
|
6
|
-
|
7
|
-
|
8
|
-
DEFAULT_VALUES = [
|
9
|
-
DEFAULT_EXCEPTS = [
|
5
|
+
describe Grape::Validations::Validators::ValuesValidator do
|
6
|
+
let_it_be(:values_model) do
|
7
|
+
Class.new do
|
8
|
+
DEFAULT_VALUES = %w[valid-type1 valid-type2 valid-type3].freeze
|
9
|
+
DEFAULT_EXCEPTS = %w[invalid-type1 invalid-type2 invalid-type3].freeze
|
10
|
+
|
10
11
|
class << self
|
11
12
|
def values
|
12
13
|
@values ||= []
|
@@ -27,214 +28,219 @@ describe Grape::Validations::ValuesValidator do
|
|
27
28
|
@excepts ||= []
|
28
29
|
@excepts << except
|
29
30
|
end
|
31
|
+
|
32
|
+
def include?(value)
|
33
|
+
values.include?(value)
|
34
|
+
end
|
30
35
|
end
|
31
36
|
end
|
37
|
+
end
|
32
38
|
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
get '/' do
|
42
|
-
{ type: params[:type] }
|
43
|
-
end
|
44
|
-
|
45
|
-
params do
|
46
|
-
optional :type, values: { value: -> { ValuesModel.values }, message: 'value does not include in values' }, default: 'valid-type2'
|
47
|
-
end
|
48
|
-
get '/lambda' do
|
49
|
-
{ type: params[:type] }
|
50
|
-
end
|
51
|
-
|
52
|
-
params do
|
53
|
-
requires :type, values: { except: ValuesModel.excepts, except_message: 'value is on exclusions list', message: 'default exclude message' }
|
54
|
-
end
|
55
|
-
get '/exclude/exclude_message'
|
56
|
-
|
57
|
-
params do
|
58
|
-
requires :type, values: { except: -> { ValuesModel.excepts }, except_message: 'value is on exclusions list' }
|
59
|
-
end
|
60
|
-
get '/exclude/lambda/exclude_message'
|
61
|
-
|
62
|
-
params do
|
63
|
-
requires :type, values: { except: ValuesModel.excepts, message: 'default exclude message' }
|
64
|
-
end
|
65
|
-
get '/exclude/fallback_message'
|
66
|
-
end
|
39
|
+
before do
|
40
|
+
stub_const('ValuesModel', values_model)
|
41
|
+
end
|
42
|
+
|
43
|
+
let_it_be(:app) do
|
44
|
+
ValuesModel = values_model
|
45
|
+
Class.new(Grape::API) do
|
46
|
+
default_format :json
|
67
47
|
|
48
|
+
resources :custom_message do
|
68
49
|
params do
|
69
|
-
requires :type, values: ValuesModel.values
|
50
|
+
requires :type, values: { value: ValuesModel.values, message: 'value does not include in values' }
|
70
51
|
end
|
71
52
|
get '/' do
|
72
53
|
{ type: params[:type] }
|
73
54
|
end
|
74
55
|
|
75
56
|
params do
|
76
|
-
|
57
|
+
optional :type, values: { value: -> { ValuesModel.values }, message: 'value does not include in values' }, default: 'valid-type2'
|
77
58
|
end
|
78
|
-
get '/
|
79
|
-
|
80
|
-
params do
|
81
|
-
optional :type, values: { value: ValuesModel.values }, default: 'valid-type2'
|
82
|
-
end
|
83
|
-
get '/default/hash/valid' do
|
59
|
+
get '/lambda' do
|
84
60
|
{ type: params[:type] }
|
85
61
|
end
|
86
62
|
|
87
63
|
params do
|
88
|
-
|
89
|
-
end
|
90
|
-
get '/default/valid' do
|
91
|
-
{ type: params[:type] }
|
64
|
+
requires :type, values: { except: ValuesModel.excepts, except_message: 'value is on exclusions list', message: 'default exclude message' }
|
92
65
|
end
|
66
|
+
get '/exclude/exclude_message'
|
93
67
|
|
94
68
|
params do
|
95
|
-
|
96
|
-
end
|
97
|
-
get '/default/except' do
|
98
|
-
{ type: params[:type] }
|
69
|
+
requires :type, values: { except: -> { ValuesModel.excepts }, except_message: 'value is on exclusions list' }
|
99
70
|
end
|
71
|
+
get '/exclude/lambda/exclude_message'
|
100
72
|
|
101
73
|
params do
|
102
|
-
|
103
|
-
end
|
104
|
-
get '/lambda' do
|
105
|
-
{ type: params[:type] }
|
74
|
+
requires :type, values: { except: ValuesModel.excepts, message: 'default exclude message' }
|
106
75
|
end
|
76
|
+
get '/exclude/fallback_message'
|
77
|
+
end
|
107
78
|
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
79
|
+
params do
|
80
|
+
requires :type, values: ValuesModel.values
|
81
|
+
end
|
82
|
+
get '/' do
|
83
|
+
{ type: params[:type] }
|
84
|
+
end
|
114
85
|
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
{ number: params[:number] }
|
120
|
-
end
|
86
|
+
params do
|
87
|
+
requires :type, values: []
|
88
|
+
end
|
89
|
+
get '/empty'
|
121
90
|
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
91
|
+
params do
|
92
|
+
optional :type, values: { value: ValuesModel.values }, default: 'valid-type2'
|
93
|
+
end
|
94
|
+
get '/default/hash/valid' do
|
95
|
+
{ type: params[:type] }
|
96
|
+
end
|
126
97
|
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
98
|
+
params do
|
99
|
+
optional :type, values: ValuesModel.values, default: 'valid-type2'
|
100
|
+
end
|
101
|
+
get '/default/valid' do
|
102
|
+
{ type: params[:type] }
|
103
|
+
end
|
133
104
|
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
105
|
+
params do
|
106
|
+
optional :type, values: { except: ValuesModel.excepts }, default: 'valid-type2'
|
107
|
+
end
|
108
|
+
get '/default/except' do
|
109
|
+
{ type: params[:type] }
|
110
|
+
end
|
140
111
|
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
112
|
+
params do
|
113
|
+
optional :type, values: -> { ValuesModel.values }, default: 'valid-type2'
|
114
|
+
end
|
115
|
+
get '/lambda' do
|
116
|
+
{ type: params[:type] }
|
117
|
+
end
|
147
118
|
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
119
|
+
params do
|
120
|
+
requires :type, values: ->(v) { ValuesModel.include? v }
|
121
|
+
end
|
122
|
+
get '/lambda_val' do
|
123
|
+
{ type: params[:type] }
|
124
|
+
end
|
154
125
|
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
126
|
+
params do
|
127
|
+
requires :number, type: Integer, values: ->(v) { v > 0 }
|
128
|
+
end
|
129
|
+
get '/lambda_int_val' do
|
130
|
+
{ number: params[:number] }
|
131
|
+
end
|
161
132
|
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
end
|
167
|
-
get '/optional_with_required_values'
|
133
|
+
params do
|
134
|
+
requires :type, values: -> { [] }
|
135
|
+
end
|
136
|
+
get '/empty_lambda'
|
168
137
|
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
138
|
+
params do
|
139
|
+
optional :type, values: ValuesModel.values, default: -> { ValuesModel.values.sample }
|
140
|
+
end
|
141
|
+
get '/default_lambda' do
|
142
|
+
{ type: params[:type] }
|
143
|
+
end
|
175
144
|
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
145
|
+
params do
|
146
|
+
optional :type, values: -> { ValuesModel.values }, default: -> { ValuesModel.values.sample }
|
147
|
+
end
|
148
|
+
get '/default_and_values_lambda' do
|
149
|
+
{ type: params[:type] }
|
150
|
+
end
|
182
151
|
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
152
|
+
params do
|
153
|
+
optional :type, type: Grape::API::Boolean, desc: 'A boolean', values: [true]
|
154
|
+
end
|
155
|
+
get '/values/optional_boolean' do
|
156
|
+
{ type: params[:type] }
|
157
|
+
end
|
189
158
|
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
159
|
+
params do
|
160
|
+
requires :type, type: Integer, desc: 'An integer', values: [10, 11], default: 10
|
161
|
+
end
|
162
|
+
get '/values/coercion' do
|
163
|
+
{ type: params[:type] }
|
164
|
+
end
|
196
165
|
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
166
|
+
params do
|
167
|
+
requires :type, type: Array[Integer], desc: 'An integer', values: [10, 11], default: 10
|
168
|
+
end
|
169
|
+
get '/values/array_coercion' do
|
170
|
+
{ type: params[:type] }
|
171
|
+
end
|
203
172
|
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
get '/mixed/value/except' do
|
208
|
-
{ type: params[:type] }
|
173
|
+
params do
|
174
|
+
optional :optional, type: Array do
|
175
|
+
requires :type, values: %w[a b]
|
209
176
|
end
|
177
|
+
end
|
178
|
+
get '/optional_with_required_values'
|
210
179
|
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
180
|
+
params do
|
181
|
+
requires :type, values: { except: ValuesModel.excepts }
|
182
|
+
end
|
183
|
+
get '/except/exclusive' do
|
184
|
+
{ type: params[:type] }
|
185
|
+
end
|
215
186
|
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
187
|
+
params do
|
188
|
+
requires :type, type: String, values: { except: ValuesModel.excepts }
|
189
|
+
end
|
190
|
+
get '/except/exclusive/type' do
|
191
|
+
{ type: params[:type] }
|
192
|
+
end
|
222
193
|
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
194
|
+
params do
|
195
|
+
requires :type, values: { except: -> { ValuesModel.excepts } }
|
196
|
+
end
|
197
|
+
get '/except/exclusive/lambda' do
|
198
|
+
{ type: params[:type] }
|
199
|
+
end
|
227
200
|
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
201
|
+
params do
|
202
|
+
requires :type, type: String, values: { except: -> { ValuesModel.excepts } }
|
203
|
+
end
|
204
|
+
get '/except/exclusive/lambda/type' do
|
205
|
+
{ type: params[:type] }
|
232
206
|
end
|
233
|
-
end
|
234
|
-
end
|
235
207
|
|
236
|
-
|
237
|
-
|
208
|
+
params do
|
209
|
+
requires :type, type: Integer, values: { except: -> { [3, 4, 5] } }
|
210
|
+
end
|
211
|
+
get '/except/exclusive/lambda/coercion' do
|
212
|
+
{ type: params[:type] }
|
213
|
+
end
|
214
|
+
|
215
|
+
params do
|
216
|
+
requires :type, type: Integer, values: { value: 1..5, except: [3] }
|
217
|
+
end
|
218
|
+
get '/mixed/value/except' do
|
219
|
+
{ type: params[:type] }
|
220
|
+
end
|
221
|
+
|
222
|
+
params do
|
223
|
+
optional :optional, type: Array[String], values: %w[a b c]
|
224
|
+
end
|
225
|
+
put '/optional_with_array_of_string_values'
|
226
|
+
|
227
|
+
params do
|
228
|
+
requires :type, values: { proc: ->(v) { ValuesModel.include? v } }
|
229
|
+
end
|
230
|
+
get '/proc' do
|
231
|
+
{ type: params[:type] }
|
232
|
+
end
|
233
|
+
|
234
|
+
params do
|
235
|
+
requires :type, values: { proc: ->(v) { ValuesModel.include? v }, message: 'failed check' }
|
236
|
+
end
|
237
|
+
get '/proc/message'
|
238
|
+
|
239
|
+
params do
|
240
|
+
optional :name, type: String, values: %w[a b], allow_blank: true
|
241
|
+
end
|
242
|
+
get '/allow_blank'
|
243
|
+
end
|
238
244
|
end
|
239
245
|
|
240
246
|
context 'with a custom validation message' do
|
@@ -251,7 +257,7 @@ describe Grape::Validations::ValuesValidator do
|
|
251
257
|
end
|
252
258
|
|
253
259
|
it 'validates against values in a proc' do
|
254
|
-
|
260
|
+
ValuesModel.add_value('valid-type4')
|
255
261
|
|
256
262
|
get('/custom_message/lambda', type: 'valid-type4')
|
257
263
|
expect(last_response.status).to eq 200
|
@@ -350,15 +356,14 @@ describe Grape::Validations::ValuesValidator do
|
|
350
356
|
end
|
351
357
|
|
352
358
|
it 'does not validate updated values without proc' do
|
353
|
-
|
354
|
-
|
359
|
+
ValuesModel.add_value('valid-type4')
|
355
360
|
get('/', type: 'valid-type4')
|
356
361
|
expect(last_response.status).to eq 400
|
357
362
|
expect(last_response.body).to eq({ error: 'type does not have a valid value' }.to_json)
|
358
363
|
end
|
359
364
|
|
360
365
|
it 'validates against values in a proc' do
|
361
|
-
|
366
|
+
ValuesModel.add_value('valid-type4')
|
362
367
|
|
363
368
|
get('/lambda', type: 'valid-type4')
|
364
369
|
expect(last_response.status).to eq 200
|
@@ -420,21 +425,21 @@ describe Grape::Validations::ValuesValidator do
|
|
420
425
|
it 'raises IncompatibleOptionValues on an invalid default value from proc' do
|
421
426
|
subject = Class.new(Grape::API)
|
422
427
|
expect do
|
423
|
-
subject.params { optional :type, values: [
|
428
|
+
subject.params { optional :type, values: %w[valid-type1 valid-type2 valid-type3], default: "#{ValuesModel.values.sample}_invalid" }
|
424
429
|
end.to raise_error Grape::Exceptions::IncompatibleOptionValues
|
425
430
|
end
|
426
431
|
|
427
432
|
it 'raises IncompatibleOptionValues on an invalid default value' do
|
428
433
|
subject = Class.new(Grape::API)
|
429
434
|
expect do
|
430
|
-
subject.params { optional :type, values: [
|
435
|
+
subject.params { optional :type, values: %w[valid-type1 valid-type2 valid-type3], default: 'invalid-type' }
|
431
436
|
end.to raise_error Grape::Exceptions::IncompatibleOptionValues
|
432
437
|
end
|
433
438
|
|
434
439
|
it 'raises IncompatibleOptionValues when type is incompatible with values array' do
|
435
440
|
subject = Class.new(Grape::API)
|
436
441
|
expect do
|
437
|
-
subject.params { optional :type, values: [
|
442
|
+
subject.params { optional :type, values: %w[valid-type1 valid-type2 valid-type3], type: Symbol }
|
438
443
|
end.to raise_error Grape::Exceptions::IncompatibleOptionValues
|
439
444
|
end
|
440
445
|
|
@@ -648,9 +653,9 @@ describe Grape::Validations::ValuesValidator do
|
|
648
653
|
end
|
649
654
|
|
650
655
|
it 'accepts multiple valid values' do
|
651
|
-
get '/proc', type: [
|
656
|
+
get '/proc', type: %w[valid-type1 valid-type3]
|
652
657
|
expect(last_response.status).to eq 200
|
653
|
-
expect(last_response.body).to eq({ type: [
|
658
|
+
expect(last_response.body).to eq({ type: %w[valid-type1 valid-type3] }.to_json)
|
654
659
|
end
|
655
660
|
|
656
661
|
it 'rejects a single invalid value' do
|
@@ -660,7 +665,7 @@ describe Grape::Validations::ValuesValidator do
|
|
660
665
|
end
|
661
666
|
|
662
667
|
it 'rejects an invalid value among valid ones' do
|
663
|
-
get '/proc', type: [
|
668
|
+
get '/proc', type: %w[valid-type1 invalid-type1 valid-type3]
|
664
669
|
expect(last_response.status).to eq 400
|
665
670
|
expect(last_response.body).to eq({ error: 'type does not have a valid value' }.to_json)
|
666
671
|
end
|