spec/grape/validations_spec.rb in grape-0.2.6 vs spec/grape/validations_spec.rb in grape-0.3.0

- old
+ new

@@ -56,10 +56,33 @@ subject.params { requires :some_param } subject.settings[:declared_params].should == [:some_param] 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 def validate_param!(attr_name, params) unless params[attr_name] == 'im custom' @@ -145,22 +168,22 @@ namespace 'double' do get 'two' do 'no validation required' end 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 last_response.body.should == 'no validation required' end