Skip to content

Commit c9c0c1f

Browse files
committed
Merge pull request #799 from bwalex/fix_778
Address #778 - don't restrict options to Hash/Array types
2 parents 907dc7e + 96cf346 commit c9c0c1f

File tree

3 files changed

+58
-21
lines changed

3 files changed

+58
-21
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* [#745](https://github.com/intridea/grape/pull/745): Added `:binary, application/octet-stream` content-type - [@akabraham](https://github.com/akabraham).
88
* [#757](https://github.com/intridea/grape/pull/757): Changed `desc` can now be used with a block syntax - [@dspaeth-faber](https://github.com/dspaeth-faber).
99
* [#779](https://github.com/intridea/grape/pull/779): Fixed using `values` with a `default` proc - [@ShPakvel](https://github.com/ShPakvel).
10+
* [#799](https://github.com/intridea/grape/pull/799): Fixed custom validators with required `Hash`, `Array` types - [@bwalex](https://github.com/bwalex).
1011
* Your contribution here.
1112

1213
0.9.0 (8/27/2014)

lib/grape/validations/params_scope.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ def validate_attributes(attrs, opts, &block)
7373

7474
def new_scope(attrs, optional = false, &block)
7575
opts = attrs[1] || { type: Array }
76-
raise ArgumentError unless opts.keys.to_set.subset? [:type].to_set
7776
ParamsScope.new(api: @api, element: attrs.first, parent: self, optional: optional, type: opts[:type], &block)
7877
end
7978

spec/grape/validations_spec.rb

Lines changed: 57 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -227,16 +227,6 @@ def define_requires_none
227227
expect(JSON.parse(last_response.body)).to eq('items' => [])
228228
end
229229

230-
it "doesn't allow any key in the options hash other than type" do
231-
expect {
232-
subject.params do
233-
requires(:items, desc: 'Foo') do
234-
requires :key
235-
end
236-
end
237-
}.to raise_error ArgumentError
238-
end
239-
240230
it 'adds to declared parameters' do
241231
subject.params do
242232
requires :items do
@@ -281,16 +271,6 @@ def define_requires_none
281271
expect(last_response.body).to eq('required works')
282272
end
283273

284-
it "doesn't allow any key in the options hash other than type" do
285-
expect {
286-
subject.params do
287-
requires(:items, desc: 'Foo') do
288-
requires :key
289-
end
290-
end
291-
}.to raise_error ArgumentError
292-
end
293-
294274
it 'adds to declared parameters' do
295275
subject.params do
296276
requires :items do
@@ -335,6 +315,63 @@ def define_requires_none
335315
end
336316
end
337317

318+
context 'custom validator for a Hash' do
319+
module DateRangeValidations
320+
class DateRangeValidator < Grape::Validations::Base
321+
def validate_param!(attr_name, params)
322+
unless params[attr_name][:from] <= params[attr_name][:to]
323+
raise Grape::Exceptions::Validation, params: [@scope.full_name(attr_name)], message: "'from' must be lower or equal to 'to'"
324+
end
325+
end
326+
end
327+
end
328+
329+
before do
330+
subject.params do
331+
optional :date_range, date_range: true, type: Hash do
332+
requires :from, type: Integer
333+
requires :to, type: Integer
334+
end
335+
end
336+
subject.get('/optional') do
337+
'optional works'
338+
end
339+
subject.params do
340+
requires :date_range, date_range: true, type: Hash do
341+
requires :from, type: Integer
342+
requires :to, type: Integer
343+
end
344+
end
345+
subject.get('/required') do
346+
'required works'
347+
end
348+
end
349+
350+
context 'which is optional' do
351+
it "doesn't throw an error if the validation passes" do
352+
get '/optional', date_range: { from: 1, to: 2 }
353+
expect(last_response.status).to eq(200)
354+
end
355+
356+
it 'errors if the validation fails' do
357+
get '/optional', date_range: { from: 2, to: 1 }
358+
expect(last_response.status).to eq(400)
359+
end
360+
end
361+
362+
context 'which is required' do
363+
it "doesn't throw an error if the validation passes" do
364+
get '/required', date_range: { from: 1, to: 2 }
365+
expect(last_response.status).to eq(200)
366+
end
367+
368+
it 'errors if the validation fails' do
369+
get '/required', date_range: { from: 2, to: 1 }
370+
expect(last_response.status).to eq(400)
371+
end
372+
end
373+
end
374+
338375
context 'validation within arrays' do
339376
before do
340377
subject.params do

0 commit comments

Comments
 (0)