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

validation for array in params #333

Merged
merged 2 commits into from
Feb 12, 2013
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.markdown
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
Next Release
============

* [#333](https://github.com/intridea/grape/pull/333): Validation for array in params - [@flyerhzm](https://github.com/flyerhzm).
* [#306](https://github.com/intridea/grape/issues/306): Added I18n support for all Grape exceptions - [@niedhui](https://github.com/niedhui).
* [#294](https://github.com/intridea/grape/issues/294): Extracted `Grape::Entity` into a [grape-entity](https://github.com/agileanimal/grape-entity) gem - [@agileanimal](https://github.com/agileanimal).
* [#309](https://github.com/intridea/grape/pull/309): An XML format API will return an error instead of returning a string representation of the response if the latter cannot be converted to XML - [@dblock](http://github.com/dblock).
Expand Down
8 changes: 5 additions & 3 deletions lib/grape/validations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@ def initialize(attrs, options, required, scope)
def validate!(params)
params = @scope.params(params)

@attrs.each do |attr_name|
if @required || params.has_key?(attr_name)
validate_param!(attr_name, params)
(params.is_a?(Array) ? params : [params]).each do |resource_params|
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor note, there's a more elegant way to write this, I think you can just do Array(params).each, it will make an array of params if params is not an array.

@attrs.each do |attr_name|
if @required || resource_params.has_key?(attr_name)
validate_param!(attr_name, resource_params)
end
end
end
end
Expand Down
29 changes: 26 additions & 3 deletions spec/grape/validations_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,29 @@ def app; subject end
end
end

context 'group' do
before do
subject.params {
group :items do
requires :key
end
}
subject.get '/required' do 'required works'; end
end

it 'errors when param not present' do
get '/required'
last_response.status.should == 400
last_response.body.should == 'missing parameter: items[key]'
end

it "doesn't throw a missing param when param is present" do
get '/required', { :items => [:key => 'hello', :key => 'world'] }
last_response.status.should == 200
last_response.body.should == 'required works'
end
end

context 'custom validation' do
module CustomValidations
class Customvalidator < Grape::Validations::Validator
Expand Down Expand Up @@ -147,19 +170,19 @@ def validate_param!(attr_name, params)
end
end
end

specify 'the parent namespace uses the validator' do
get '/nested/one', { :custom => 'im wrong, validate me'}
last_response.status.should == 400
last_response.body.should == 'custom: is not custom!'
end

specify 'the nested namesapce inherits the custom validator' do
get '/nested/nested/two', { :custom => 'im wrong, validate me'}
last_response.status.should == 400
last_response.body.should == 'custom: is not custom!'
end

specify 'peer namesapces does not have the validator' do
get '/peer/one', { :custom => 'im not validated' }
last_response.status.should == 200
Expand Down