Skip to content

Commit 3e5e884

Browse files
committed
added more tests and fixed params validation when value or except in values is a proc
1 parent a5af36b commit 3e5e884

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

lib/grape/validations/params_scope.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,10 @@ def validate(type, options, attrs, doc_attrs)
349349
def validate_value_coercion(coerce_type, values)
350350
return unless coerce_type && values
351351
return if values.is_a?(Proc)
352+
if values.is_a?(Hash)
353+
return values[:value] && values[:value].is_a?(Proc)
354+
return if values[:except] && values[:except].is_a?(Proc)
355+
end
352356
coerce_type = coerce_type.first if coerce_type.is_a?(Array)
353357
value_types = values.is_a?(Range) ? [values.begin, values.end] : values
354358
if coerce_type == Virtus::Attribute::Boolean

spec/grape/validations/params_scope_spec.rb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,36 @@ def initialize(value)
157157
end
158158
end
159159

160+
context 'coercing values validation with proc' do
161+
it 'allows the proc to pass validation without checking' do
162+
subject.params { requires :numbers, type: Integer, values: -> { [0,1,2] } }
163+
164+
subject.post('/required') { 'coercion with proc works' }
165+
post '/required', numbers: '1'
166+
expect(last_response.status).to eq(201)
167+
expect(last_response.body).to eq('coercion with proc works')
168+
end
169+
170+
it 'allows the proc to pass validation without checking in value' do
171+
subject.params { requires :numbers, type: Integer, values: { value: -> { [0,1,2] } } }
172+
173+
subject.post('/required') { 'coercion with proc works' }
174+
post '/required', numbers: '1'
175+
expect(last_response.status).to eq(201)
176+
expect(last_response.body).to eq('coercion with proc works')
177+
end
178+
179+
it 'allows the proc to pass validation without checking in except' do
180+
subject.params { requires :numbers, type: Integer, values: { except: -> { [0,1,2] } } }
181+
182+
subject.post('/required') { 'coercion with proc works' }
183+
post '/required', numbers: '10'
184+
p last_response.body
185+
expect(last_response.status).to eq(201)
186+
expect(last_response.body).to eq('coercion with proc works')
187+
end
188+
end
189+
160190
context 'with range values' do
161191
context "when left range endpoint isn't #kind_of? the type" do
162192
it 'raises exception' do

spec/grape/validations/validators/values_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@ def app
438438
it 'allows any other value outside excepts' do
439439
get '/except/exclusive/lambda/coercion', type: '10010000'
440440
expect(last_response.status).to eq 200
441-
expect(last_response.body).to eq({ type: 'value' }.to_json)
441+
expect(last_response.body).to eq({ type: 10010000 }.to_json)
442442
end
443443

444444
it 'rejects values that matches except' do

0 commit comments

Comments
 (0)