Skip to content

Commit

Permalink
Address ruby-grape#778 - don't restrict options to Hash/Array types
Browse files Browse the repository at this point in the history
 * I'm not sure what I was thinking originally, but this obviously
   breaks the use of custom validators.

 * Add a spec for ruby-grape#778 whilst on it.
  • Loading branch information
bwalex committed Nov 2, 2014
1 parent 907dc7e commit 1351d66
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 21 deletions.
1 change: 0 additions & 1 deletion lib/grape/validations/params_scope.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ def validate_attributes(attrs, opts, &block)

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

Expand Down
77 changes: 57 additions & 20 deletions spec/grape/validations_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -227,16 +227,6 @@ def define_requires_none
expect(JSON.parse(last_response.body)).to eq('items' => [])
end

it "doesn't allow any key in the options hash other than type" do
expect {
subject.params do
requires(:items, desc: 'Foo') do
requires :key
end
end
}.to raise_error ArgumentError
end

it 'adds to declared parameters' do
subject.params do
requires :items do
Expand Down Expand Up @@ -281,16 +271,6 @@ def define_requires_none
expect(last_response.body).to eq('required works')
end

it "doesn't allow any key in the options hash other than type" do
expect {
subject.params do
requires(:items, desc: 'Foo') do
requires :key
end
end
}.to raise_error ArgumentError
end

it 'adds to declared parameters' do
subject.params do
requires :items do
Expand Down Expand Up @@ -335,6 +315,63 @@ def define_requires_none
end
end

context 'custom validator for a Hash' do
module DateRangeValidations
class DateRangeValidator < Grape::Validations::Base
def validate_param!(attr_name, params)
unless params[attr_name][:from] <= params[attr_name][:to]
raise Grape::Exceptions::Validation, params: [@scope.full_name(attr_name)], message: "'from' must be lower or equal to 'to'"
end
end
end
end

before do
subject.params do
optional :date_range, date_range: true, type: Hash do
requires :from, type: Integer
requires :to, type: Integer
end
end
subject.get('/optional') do
'optional works'
end
subject.params do
requires :date_range, date_range: true, type: Hash do
requires :from, type: Integer
requires :to, type: Integer
end
end
subject.get('/required') do
'required works'
end
end

context 'which is optional' do
it "doesn't throw an error if the validation passes" do
get '/optional', date_range: { from: 1, to: 2 }
expect(last_response.status).to eq(200)
end

it 'errors if the validation fails' do
get '/optional', date_range: { from: 2, to: 1 }
expect(last_response.status).to eq(400)
end
end

context 'which is required' do
it "doesn't throw an error if the validation passes" do
get '/required', date_range: { from: 1, to: 2 }
expect(last_response.status).to eq(200)
end

it 'errors if the validation fails' do
get '/required', date_range: { from: 2, to: 1 }
expect(last_response.status).to eq(400)
end
end
end

context 'validation within arrays' do
before do
subject.params do
Expand Down

0 comments on commit 1351d66

Please sign in to comment.