diff --git a/CHANGELOG.markdown b/CHANGELOG.markdown index 487c3ece1b..e37a884a53 100644 --- a/CHANGELOG.markdown +++ b/CHANGELOG.markdown @@ -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). diff --git a/lib/grape/validations.rb b/lib/grape/validations.rb index 25bce2cf4c..c820737ce8 100644 --- a/lib/grape/validations.rb +++ b/lib/grape/validations.rb @@ -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 diff --git a/spec/grape/validations_spec.rb b/spec/grape/validations_spec.rb index edb59f5fb2..a92168f842 100644 --- a/spec/grape/validations_spec.rb +++ b/spec/grape/validations_spec.rb @@ -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 @@ -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