grape 1.6.0 → 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 +26 -0
- data/CONTRIBUTING.md +2 -1
- data/README.md +9 -1
- data/UPGRADING.md +4 -4
- data/lib/grape/api.rb +12 -0
- data/lib/grape/dsl/headers.rb +5 -2
- data/lib/grape/dsl/helpers.rb +1 -1
- data/lib/grape/middleware/auth/dsl.rb +7 -1
- data/lib/grape/middleware/base.rb +1 -1
- data/lib/grape/util/strict_hash_configuration.rb +1 -1
- data/lib/grape/validations/validators/all_or_none.rb +7 -5
- data/lib/grape/validations/validators/allow_blank.rb +9 -7
- data/lib/grape/validations/validators/as.rb +7 -5
- data/lib/grape/validations/validators/at_least_one_of.rb +6 -4
- data/lib/grape/validations/validators/base.rb +73 -71
- data/lib/grape/validations/validators/coerce.rb +63 -75
- data/lib/grape/validations/validators/default.rb +36 -34
- data/lib/grape/validations/validators/exactly_one_of.rb +8 -6
- data/lib/grape/validations/validators/except_values.rb +13 -11
- data/lib/grape/validations/validators/multiple_params_base.rb +24 -22
- data/lib/grape/validations/validators/mutual_exclusion.rb +7 -5
- data/lib/grape/validations/validators/presence.rb +6 -4
- data/lib/grape/validations/validators/regexp.rb +7 -5
- data/lib/grape/validations/validators/same_as.rb +17 -15
- data/lib/grape/validations/validators/values.rb +59 -57
- data/lib/grape/validations.rb +6 -0
- data/lib/grape/version.rb +1 -1
- data/lib/grape.rb +2 -0
- data/spec/grape/api/custom_validations_spec.rb +77 -46
- 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/shared_helpers_exactly_one_of_spec.rb +9 -15
- data/spec/grape/api_remount_spec.rb +16 -15
- data/spec/grape/api_spec.rb +317 -193
- data/spec/grape/dsl/callbacks_spec.rb +1 -0
- 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 +6 -4
- data/spec/grape/dsl/logger_spec.rb +16 -18
- data/spec/grape/dsl/middleware_spec.rb +1 -0
- data/spec/grape/dsl/parameters_spec.rb +1 -0
- data/spec/grape/dsl/request_response_spec.rb +1 -0
- data/spec/grape/dsl/routing_spec.rb +9 -6
- data/spec/grape/endpoint/declared_spec.rb +12 -12
- data/spec/grape/endpoint_spec.rb +59 -50
- data/spec/grape/entity_spec.rb +13 -13
- 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 +1 -1
- data/spec/grape/loading_spec.rb +8 -8
- data/spec/grape/middleware/auth/dsl_spec.rb +14 -5
- data/spec/grape/middleware/auth/strategies_spec.rb +60 -20
- data/spec/grape/middleware/base_spec.rb +24 -15
- data/spec/grape/middleware/error_spec.rb +1 -0
- data/spec/grape/middleware/exception_spec.rb +111 -161
- data/spec/grape/middleware/formatter_spec.rb +25 -4
- data/spec/grape/middleware/globals_spec.rb +7 -4
- data/spec/grape/middleware/stack_spec.rb +11 -11
- 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 +1 -0
- data/spec/grape/validations/params_scope_spec.rb +9 -7
- data/spec/grape/validations/single_attribute_iterator_spec.rb +1 -0
- data/spec/grape/validations/types/primitive_coercer_spec.rb +2 -2
- 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 +10 -12
- data/spec/grape/validations/validators/default_spec.rb +72 -78
- data/spec/grape/validations/validators/exactly_one_of_spec.rb +71 -77
- data/spec/grape/validations/validators/except_values_spec.rb +1 -1
- 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 +172 -171
- data/spec/grape/validations_spec.rb +45 -16
- data/spec/integration/eager_load/eager_load_spec.rb +2 -2
- 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 +10 -7
- data/spec/spec_helper.rb +11 -1
- metadata +102 -102
@@ -2,53 +2,47 @@
|
|
2
2
|
|
3
3
|
require 'spec_helper'
|
4
4
|
|
5
|
-
describe Grape::Validations::RegexpValidator do
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
default_format :json
|
10
|
-
|
11
|
-
resources :custom_message do
|
12
|
-
params do
|
13
|
-
requires :name, regexp: { value: /^[a-z]+$/, message: 'format is invalid' }
|
14
|
-
end
|
15
|
-
get do
|
16
|
-
end
|
17
|
-
|
18
|
-
params do
|
19
|
-
requires :names, type: { value: Array[String], message: 'can\'t be nil' }, regexp: { value: /^[a-z]+$/, message: 'format is invalid' }
|
20
|
-
end
|
21
|
-
get 'regexp_with_array' do
|
22
|
-
end
|
23
|
-
end
|
5
|
+
describe Grape::Validations::Validators::RegexpValidator do
|
6
|
+
let_it_be(:app) do
|
7
|
+
Class.new(Grape::API) do
|
8
|
+
default_format :json
|
24
9
|
|
10
|
+
resources :custom_message do
|
25
11
|
params do
|
26
|
-
requires :name, regexp: /^[a-z]
|
12
|
+
requires :name, regexp: { value: /^[a-z]+$/, message: 'format is invalid' }
|
27
13
|
end
|
28
14
|
get do
|
29
15
|
end
|
30
16
|
|
31
17
|
params do
|
32
|
-
requires :names, type: Array[String], regexp: /^[a-z]
|
18
|
+
requires :names, type: { value: Array[String], message: 'can\'t be nil' }, regexp: { value: /^[a-z]+$/, message: 'format is invalid' }
|
33
19
|
end
|
34
20
|
get 'regexp_with_array' do
|
35
21
|
end
|
22
|
+
end
|
36
23
|
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
24
|
+
params do
|
25
|
+
requires :name, regexp: /^[a-z]+$/
|
26
|
+
end
|
27
|
+
get do
|
28
|
+
end
|
29
|
+
|
30
|
+
params do
|
31
|
+
requires :names, type: Array[String], regexp: /^[a-z]+$/
|
32
|
+
end
|
33
|
+
get 'regexp_with_array' do
|
34
|
+
end
|
35
|
+
|
36
|
+
params do
|
37
|
+
requires :people, type: Hash do
|
38
|
+
requires :names, type: Array[String], regexp: /^[a-z]+$/
|
43
39
|
end
|
44
40
|
end
|
41
|
+
get 'nested_regexp_with_array' do
|
42
|
+
end
|
45
43
|
end
|
46
44
|
end
|
47
45
|
|
48
|
-
def app
|
49
|
-
ValidationsSpec::RegexpValidatorSpec::API
|
50
|
-
end
|
51
|
-
|
52
46
|
context 'custom validation message' do
|
53
47
|
context 'with invalid input' do
|
54
48
|
it 'refuses inapppopriate' do
|
@@ -2,31 +2,25 @@
|
|
2
2
|
|
3
3
|
require 'spec_helper'
|
4
4
|
|
5
|
-
describe Grape::Validations::SameAsValidator do
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
end
|
5
|
+
describe Grape::Validations::Validators::SameAsValidator do
|
6
|
+
let_it_be(:app) do
|
7
|
+
Class.new(Grape::API) do
|
8
|
+
params do
|
9
|
+
requires :password
|
10
|
+
requires :password_confirmation, same_as: :password
|
11
|
+
end
|
12
|
+
post do
|
13
|
+
end
|
15
14
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
end
|
15
|
+
params do
|
16
|
+
requires :password
|
17
|
+
requires :password_confirmation, same_as: { value: :password, message: 'not match' }
|
18
|
+
end
|
19
|
+
post '/custom-message' do
|
22
20
|
end
|
23
21
|
end
|
24
22
|
end
|
25
23
|
|
26
|
-
def app
|
27
|
-
ValidationsSpec::SameAsValidatorSpec::API
|
28
|
-
end
|
29
|
-
|
30
24
|
describe '/' do
|
31
25
|
context 'is the same' do
|
32
26
|
it do
|
@@ -2,11 +2,12 @@
|
|
2
2
|
|
3
3
|
require 'spec_helper'
|
4
4
|
|
5
|
-
describe Grape::Validations::ValuesValidator do
|
6
|
-
|
7
|
-
|
5
|
+
describe Grape::Validations::Validators::ValuesValidator do
|
6
|
+
let_it_be(:values_model) do
|
7
|
+
Class.new do
|
8
8
|
DEFAULT_VALUES = %w[valid-type1 valid-type2 valid-type3].freeze
|
9
9
|
DEFAULT_EXCEPTS = %w[invalid-type1 invalid-type2 invalid-type3].freeze
|
10
|
+
|
10
11
|
class << self
|
11
12
|
def values
|
12
13
|
@values ||= []
|
@@ -33,212 +34,213 @@ describe Grape::Validations::ValuesValidator do
|
|
33
34
|
end
|
34
35
|
end
|
35
36
|
end
|
37
|
+
end
|
36
38
|
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
get '/' do
|
46
|
-
{ type: params[:type] }
|
47
|
-
end
|
48
|
-
|
49
|
-
params do
|
50
|
-
optional :type, values: { value: -> { ValuesModel.values }, message: 'value does not include in values' }, default: 'valid-type2'
|
51
|
-
end
|
52
|
-
get '/lambda' do
|
53
|
-
{ type: params[:type] }
|
54
|
-
end
|
55
|
-
|
56
|
-
params do
|
57
|
-
requires :type, values: { except: ValuesModel.excepts, except_message: 'value is on exclusions list', message: 'default exclude message' }
|
58
|
-
end
|
59
|
-
get '/exclude/exclude_message'
|
60
|
-
|
61
|
-
params do
|
62
|
-
requires :type, values: { except: -> { ValuesModel.excepts }, except_message: 'value is on exclusions list' }
|
63
|
-
end
|
64
|
-
get '/exclude/lambda/exclude_message'
|
65
|
-
|
66
|
-
params do
|
67
|
-
requires :type, values: { except: ValuesModel.excepts, message: 'default exclude message' }
|
68
|
-
end
|
69
|
-
get '/exclude/fallback_message'
|
70
|
-
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
|
71
47
|
|
48
|
+
resources :custom_message do
|
72
49
|
params do
|
73
|
-
requires :type, values: ValuesModel.values
|
50
|
+
requires :type, values: { value: ValuesModel.values, message: 'value does not include in values' }
|
74
51
|
end
|
75
52
|
get '/' do
|
76
53
|
{ type: params[:type] }
|
77
54
|
end
|
78
55
|
|
79
56
|
params do
|
80
|
-
|
57
|
+
optional :type, values: { value: -> { ValuesModel.values }, message: 'value does not include in values' }, default: 'valid-type2'
|
81
58
|
end
|
82
|
-
get '/
|
83
|
-
|
84
|
-
params do
|
85
|
-
optional :type, values: { value: ValuesModel.values }, default: 'valid-type2'
|
86
|
-
end
|
87
|
-
get '/default/hash/valid' do
|
59
|
+
get '/lambda' do
|
88
60
|
{ type: params[:type] }
|
89
61
|
end
|
90
62
|
|
91
63
|
params do
|
92
|
-
|
93
|
-
end
|
94
|
-
get '/default/valid' do
|
95
|
-
{ type: params[:type] }
|
64
|
+
requires :type, values: { except: ValuesModel.excepts, except_message: 'value is on exclusions list', message: 'default exclude message' }
|
96
65
|
end
|
66
|
+
get '/exclude/exclude_message'
|
97
67
|
|
98
68
|
params do
|
99
|
-
|
100
|
-
end
|
101
|
-
get '/default/except' do
|
102
|
-
{ type: params[:type] }
|
69
|
+
requires :type, values: { except: -> { ValuesModel.excepts }, except_message: 'value is on exclusions list' }
|
103
70
|
end
|
71
|
+
get '/exclude/lambda/exclude_message'
|
104
72
|
|
105
73
|
params do
|
106
|
-
|
107
|
-
end
|
108
|
-
get '/lambda' do
|
109
|
-
{ type: params[:type] }
|
74
|
+
requires :type, values: { except: ValuesModel.excepts, message: 'default exclude message' }
|
110
75
|
end
|
76
|
+
get '/exclude/fallback_message'
|
77
|
+
end
|
111
78
|
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
79
|
+
params do
|
80
|
+
requires :type, values: ValuesModel.values
|
81
|
+
end
|
82
|
+
get '/' do
|
83
|
+
{ type: params[:type] }
|
84
|
+
end
|
118
85
|
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
{ number: params[:number] }
|
124
|
-
end
|
86
|
+
params do
|
87
|
+
requires :type, values: []
|
88
|
+
end
|
89
|
+
get '/empty'
|
125
90
|
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
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
|
130
97
|
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
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
|
137
104
|
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
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
|
144
111
|
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
112
|
+
params do
|
113
|
+
optional :type, values: -> { ValuesModel.values }, default: 'valid-type2'
|
114
|
+
end
|
115
|
+
get '/lambda' do
|
116
|
+
{ type: params[:type] }
|
117
|
+
end
|
151
118
|
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
119
|
+
params do
|
120
|
+
requires :type, values: ->(v) { ValuesModel.include? v }
|
121
|
+
end
|
122
|
+
get '/lambda_val' do
|
123
|
+
{ type: params[:type] }
|
124
|
+
end
|
158
125
|
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
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
|
165
132
|
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
end
|
171
|
-
get '/optional_with_required_values'
|
133
|
+
params do
|
134
|
+
requires :type, values: -> { [] }
|
135
|
+
end
|
136
|
+
get '/empty_lambda'
|
172
137
|
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
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
|
179
144
|
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
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
|
186
151
|
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
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
|
193
158
|
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
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
|
200
165
|
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
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
|
207
172
|
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
get '/mixed/value/except' do
|
212
|
-
{ type: params[:type] }
|
173
|
+
params do
|
174
|
+
optional :optional, type: Array do
|
175
|
+
requires :type, values: %w[a b]
|
213
176
|
end
|
177
|
+
end
|
178
|
+
get '/optional_with_required_values'
|
214
179
|
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
180
|
+
params do
|
181
|
+
requires :type, values: { except: ValuesModel.excepts }
|
182
|
+
end
|
183
|
+
get '/except/exclusive' do
|
184
|
+
{ type: params[:type] }
|
185
|
+
end
|
219
186
|
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
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
|
226
193
|
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
194
|
+
params do
|
195
|
+
requires :type, values: { except: -> { ValuesModel.excepts } }
|
196
|
+
end
|
197
|
+
get '/except/exclusive/lambda' do
|
198
|
+
{ type: params[:type] }
|
199
|
+
end
|
231
200
|
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
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] }
|
236
206
|
end
|
237
|
-
end
|
238
|
-
end
|
239
207
|
|
240
|
-
|
241
|
-
|
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
|
242
244
|
end
|
243
245
|
|
244
246
|
context 'with a custom validation message' do
|
@@ -255,7 +257,7 @@ describe Grape::Validations::ValuesValidator do
|
|
255
257
|
end
|
256
258
|
|
257
259
|
it 'validates against values in a proc' do
|
258
|
-
|
260
|
+
ValuesModel.add_value('valid-type4')
|
259
261
|
|
260
262
|
get('/custom_message/lambda', type: 'valid-type4')
|
261
263
|
expect(last_response.status).to eq 200
|
@@ -354,15 +356,14 @@ describe Grape::Validations::ValuesValidator do
|
|
354
356
|
end
|
355
357
|
|
356
358
|
it 'does not validate updated values without proc' do
|
357
|
-
|
358
|
-
|
359
|
+
ValuesModel.add_value('valid-type4')
|
359
360
|
get('/', type: 'valid-type4')
|
360
361
|
expect(last_response.status).to eq 400
|
361
362
|
expect(last_response.body).to eq({ error: 'type does not have a valid value' }.to_json)
|
362
363
|
end
|
363
364
|
|
364
365
|
it 'validates against values in a proc' do
|
365
|
-
|
366
|
+
ValuesModel.add_value('valid-type4')
|
366
367
|
|
367
368
|
get('/lambda', type: 'valid-type4')
|
368
369
|
expect(last_response.status).to eq 200
|
@@ -424,7 +425,7 @@ describe Grape::Validations::ValuesValidator do
|
|
424
425
|
it 'raises IncompatibleOptionValues on an invalid default value from proc' do
|
425
426
|
subject = Class.new(Grape::API)
|
426
427
|
expect do
|
427
|
-
subject.params { optional :type, values: %w[valid-type1 valid-type2 valid-type3], default: "#{
|
428
|
+
subject.params { optional :type, values: %w[valid-type1 valid-type2 valid-type3], default: "#{ValuesModel.values.sample}_invalid" }
|
428
429
|
end.to raise_error Grape::Exceptions::IncompatibleOptionValues
|
429
430
|
end
|
430
431
|
|