spec/grape/validations_spec.rb in grape-0.8.0 vs spec/grape/validations_spec.rb in grape-0.9.0

- old
+ new

@@ -657,11 +657,11 @@ context 'custom validation' do module CustomValidations class Customvalidator < Grape::Validations::Validator def validate_param!(attr_name, params) unless params[attr_name] == 'im custom' - raise Grape::Exceptions::Validation, param: @scope.full_name(attr_name), message: "is not custom!" + raise Grape::Exceptions::Validation, params: [@scope.full_name(attr_name)], message: "is not custom!" end end end end @@ -800,10 +800,41 @@ get '/unrelated/double/two' expect(last_response.status).to eq(200) end end end + + context 'when using options on param' do + module CustomValidations + class CustomvalidatorWithOptions < Grape::Validations::SingleOptionValidator + def validate_param!(attr_name, params) + unless params[attr_name] == @option[:text] + raise Grape::Exceptions::Validation, params: [@scope.full_name(attr_name)], message: @option[:error_message] + end + end + end + end + + before do + subject.params do + optional :custom, customvalidator_with_options: { text: 'im custom with options', error_message: "is not custom with options!" } + end + subject.get '/optional_custom' do + 'optional with custom works!' + end + end + + it 'validates param with custom validator with options' do + get '/optional_custom', custom: 'im custom with options' + expect(last_response.status).to eq(200) + expect(last_response.body).to eq('optional with custom works!') + + get '/optional_custom', custom: 'im wrong' + expect(last_response.status).to eq(400) + expect(last_response.body).to eq('custom is not custom with options!') + end + end end # end custom validation context 'named' do context 'can be defined' do it 'in helpers' do @@ -811,24 +842,24 @@ params :pagination do end end end - it 'in helper module which kind of Grape::API::Helpers' do + it 'in helper module which kind of Grape::DSL::Helpers::BaseHelper' do module SharedParams - extend Grape::API::Helpers + extend Grape::DSL::Helpers::BaseHelper params :pagination do end end subject.helpers SharedParams end end context 'can be included in usual params' do before do module SharedParams - extend Grape::API::Helpers + extend Grape::DSL::Helpers::BaseHelper params :period do optional :start_date optional :end_date end end @@ -927,11 +958,11 @@ 'mutually_exclusive works!' end get '/mutually_exclusive', beer: 'string', wine: 'anotherstring' expect(last_response.status).to eq(400) - expect(last_response.body).to eq("[:beer, :wine] are mutually exclusive") + expect(last_response.body).to eq "beer, wine are mutually exclusive" end end context 'more than one set of mutually exclusive params' do it 'errors for all sets' do @@ -947,47 +978,78 @@ 'mutually_exclusive works!' end get '/mutually_exclusive', beer: 'true', wine: 'true', scotch: 'true', aquavit: 'true' expect(last_response.status).to eq(400) - expect(last_response.body).to match(/\[:beer, :wine\] are mutually exclusive/) - expect(last_response.body).to match(/\[:scotch, :aquavit\] are mutually exclusive/) + expect(last_response.body).to eq "beer, wine are mutually exclusive, scotch, aquavit are mutually exclusive" end end end context 'exactly one of' do context 'params' do - it 'errors when two or more are present' do + before :each do subject.params do optional :beer optional :wine optional :juice exactly_one_of :beer, :wine, :juice end subject.get '/exactly_one_of' do 'exactly_one_of works!' end + end + it 'errors when none are present' do + get '/exactly_one_of' + expect(last_response.status).to eq(400) + expect(last_response.body).to eq "beer, wine, juice are missing, exactly one parameter must be provided" + end + + it 'succeeds when one is present' do + get '/exactly_one_of', beer: 'string' + expect(last_response.status).to eq(200) + expect(last_response.body).to eq 'exactly_one_of works!' + end + + it 'errors when two or more are present' do get '/exactly_one_of', beer: 'string', wine: 'anotherstring' expect(last_response.status).to eq(400) - expect(last_response.body).to eq("[:beer, :wine] are mutually exclusive") + expect(last_response.body).to eq "beer, wine are mutually exclusive" end + end + end - it 'errors when none is selected' do + context 'at least one of' do + context 'params' do + before :each do subject.params do optional :beer optional :wine optional :juice - exactly_one_of :beer, :wine, :juice + at_least_one_of :beer, :wine, :juice end - subject.get '/exactly_one_of' do - 'exactly_one_of works!' + subject.get '/at_least_one_of' do + 'at_least_one_of works!' end + end - get '/exactly_one_of' + it 'errors when none are present' do + get '/at_least_one_of' expect(last_response.status).to eq(400) - expect(last_response.body).to eq("[:beer, :wine, :juice] - exactly one parameter must be provided") + expect(last_response.body).to eq "beer, wine, juice are missing, at least one parameter must be provided" + end + + it 'does not error when one is present' do + get '/at_least_one_of', beer: 'string' + expect(last_response.status).to eq(200) + expect(last_response.body).to eq 'at_least_one_of works!' + end + + it 'does not error when two are present' do + get '/at_least_one_of', beer: 'string', wine: 'string' + expect(last_response.status).to eq(200) + expect(last_response.body).to eq 'at_least_one_of works!' end end end end end