Skip to content

Commit

Permalink
Merge pull request #333 from flyerhzm/array_params
Browse files Browse the repository at this point in the history
validation for array in params
  • Loading branch information
dblock committed Feb 12, 2013
2 parents f16d66c + b10c616 commit 87f7a7f
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 6 deletions.
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|
@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

0 comments on commit 87f7a7f

Please sign in to comment.