spec/grape/validations_spec.rb in grape-0.5.0 vs spec/grape/validations_spec.rb in grape-0.6.0

- old
+ new

@@ -10,11 +10,11 @@ subject.params { optional :a_number, :regexp => /^[0-9]+$/ } subject.get '/optional' do 'optional works!'; end get '/optional', { :a_number => 'string' } last_response.status.should == 400 - last_response.body.should == 'invalid parameter: a_number' + last_response.body.should == 'a_number is invalid' get '/optional', { :a_number => 45 } last_response.status.should == 200 last_response.body.should == 'optional works!' end @@ -41,11 +41,11 @@ end it 'errors when param not present' do get '/required' last_response.status.should == 400 - last_response.body.should == 'missing parameter: key' + last_response.body.should == 'key is missing' end it "doesn't throw a missing param when param is present" do get '/required', { :key => 'cool' } last_response.status.should == 200 @@ -56,10 +56,52 @@ subject.params { requires :some_param } subject.settings[:declared_params].should == [:some_param] end end + context 'required with a block' do + before do + subject.params { + requires :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 == 'items[key] is missing' + 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 + + it "doesn't allow more than one parameter" do + expect { + subject.params { + requires(:items, desc: 'Foo') do + requires :key + end + } + }.to raise_error ArgumentError + end + + it 'adds to declared parameters' do + subject.params { + requires :items do + requires :key + end + } + subject.settings[:declared_params].should == [:items => [:key]] + end + end + context 'group' do before do subject.params { group :items do requires :key @@ -69,11 +111,11 @@ end it 'errors when param not present' do get '/required' last_response.status.should == 400 - last_response.body.should == 'missing parameter: items[key]' + last_response.body.should == 'items[key] is missing' 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 @@ -88,16 +130,118 @@ } subject.settings[:declared_params].should == [:items => [:key]] end end + context 'optional with a block' do + before do + subject.params { + optional :items do + requires :key + end + } + subject.get '/optional_group' do 'optional group works'; end + end + + it "doesn't throw a missing param when the group isn't present" do + get '/optional_group' + last_response.status.should == 200 + last_response.body.should == 'optional group works' + end + + it "doesn't throw a missing param when both group and param are given" do + get '/optional_group', { :items => {:key => 'foo'} } + last_response.status.should == 200 + last_response.body.should == 'optional group works' + end + + it "errors when group is present, but required param is not" do + get '/optional_group', { :items => {:NOT_key => 'foo'} } + last_response.status.should == 400 + last_response.body.should == 'items[key] is missing' + end + + it 'adds to declared parameters' do + subject.params { + optional :items do + requires :key + end + } + subject.settings[:declared_params].should == [:items => [:key]] + end + end + + context 'nested optional blocks' do + before do + subject.params { + optional :items do + requires :key + optional(:optional_subitems) { requires :value } + requires(:required_subitems) { requires :value } + end + } + subject.get('/nested_optional_group') { 'nested optional group works' } + end + + it 'does no internal validations if the outer group is blank' do + get '/nested_optional_group' + last_response.status.should == 200 + last_response.body.should == 'nested optional group works' + end + + it 'does internal validations if the outer group is present' do + get '/nested_optional_group', { :items => {:key => 'foo' }} + last_response.status.should == 400 + last_response.body.should == 'items[required_subitems][value] is missing' + + get '/nested_optional_group', { :items => { :key => 'foo', :required_subitems => {:value => 'bar'}}} + last_response.status.should == 200 + last_response.body.should == 'nested optional group works' + end + + it 'handles deep nesting' do + get '/nested_optional_group', { :items => { :key => 'foo', :required_subitems => {:value => 'bar'}, :optional_subitems => {:NOT_value => 'baz'}}} + last_response.status.should == 400 + last_response.body.should == 'items[optional_subitems][value] is missing' + + get '/nested_optional_group', { :items => { :key => 'foo', :required_subitems => {:value => 'bar'}, :optional_subitems => {:value => 'baz'}}} + last_response.status.should == 200 + last_response.body.should == 'nested optional group works' + end + + it 'adds to declared parameters' do + subject.params { + optional :items do + requires :key + optional(:optional_subitems) { requires :value } + requires(:required_subitems) { requires :value } + end + } + subject.settings[:declared_params].should == [:items => [:key, {:optional_subitems => [:value]}, {:required_subitems => [:value]}]] + end + end + + context 'multiple validation errors' do + before do + subject.params { requires :yolo; requires :swag } + subject.get '/two_required' do 'two required works'; end + end + + it 'throws the validation errors' do + get '/two_required' + last_response.status.should == 400 + last_response.body.should =~ /yolo is missing/ + last_response.body.should =~ /swag is missing/ + 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' - throw :error, :status => 400, :message => "#{attr_name}: is not custom!" + raise Grape::Exceptions::Validation, :param => @scope.full_name(attr_name), :message => "is not custom!" end end end end @@ -112,11 +256,11 @@ last_response.status.should == 200 last_response.body.should == 'optional with custom works!' get '/optional_custom', { :custom => 'im wrong' } last_response.status.should == 400 - last_response.body.should == 'custom: is not custom!' + last_response.body.should == 'custom is not custom!' end it "skips validation when parameter isn't present" do get '/optional_custom' last_response.status.should == 200 @@ -126,11 +270,11 @@ it 'validates with custom validator when param present and incorrect type' do subject.params { optional :custom, :type => String, :customvalidator => true } get '/optional_custom', { :custom => 123 } last_response.status.should == 400 - last_response.body.should == 'custom: is not custom!' + last_response.body.should == 'custom is not custom!' end end context 'when using requires with a custom validator' do before do @@ -139,21 +283,21 @@ end it 'validates when param is present' do get '/required_custom', { :custom => 'im wrong, validate me' } last_response.status.should == 400 - last_response.body.should == 'custom: is not custom!' + last_response.body.should == 'custom is not custom!' get '/required_custom', { :custom => 'im custom' } last_response.status.should == 200 last_response.body.should == 'required with custom works!' end it 'validates when param is not present' do get '/required_custom' last_response.status.should == 400 - last_response.body.should == 'missing parameter: custom' + last_response.body.should == 'custom is missing, custom is not custom!' end context 'nested namespaces' do before do subject.params { requires :custom, :customvalidator => true } @@ -181,16 +325,16 @@ 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!' + 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!' + 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