Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Address #778 - don't restrict options to Hash/Array types #799

Merged
merged 2 commits into from
Nov 2, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* [#745](https://github.com/intridea/grape/pull/745): Added `:binary, application/octet-stream` content-type - [@akabraham](https://github.com/akabraham).
* [#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).
* [#779](https://github.com/intridea/grape/pull/779): Fixed using `values` with a `default` proc - [@ShPakvel](https://github.com/ShPakvel).
* [#799](https://github.com/intridea/grape/pull/799): Fixed custom validators with required `Hash`, `Array` types - [@bwalex](https://github.com/bwalex).
* Your contribution here.

0.9.0 (8/27/2014)
Expand Down
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