spec/grape/validations_spec.rb in grape-0.10.1 vs spec/grape/validations_spec.rb in grape-0.11.0

- old
+ new

@@ -45,10 +45,48 @@ end expect(subject.route_setting(:declared_params)).to eq([:some_param]) end end + context 'optional using Grape::Entity documentation' do + def define_optional_using + documentation = { field_a: { type: String }, field_b: { type: String } } + subject.params do + optional :all, using: documentation + end + end + before do + define_optional_using + subject.get '/optional' do + 'optional with using works' + end + end + + it 'adds entity documentation to declared params' do + define_optional_using + expect(subject.route_setting(:declared_params)).to eq([:field_a, :field_b]) + end + + it 'works when field_a and field_b are not present' do + get '/optional' + expect(last_response.status).to eq(200) + expect(last_response.body).to eq('optional with using works') + end + + it 'works when field_a is present' do + get '/optional', field_a: 'woof' + expect(last_response.status).to eq(200) + expect(last_response.body).to eq('optional with using works') + end + + it 'works when field_b is present' do + get '/optional', field_b: 'woof' + expect(last_response.status).to eq(200) + expect(last_response.body).to eq('optional with using works') + end + end + context 'required' do before do subject.params do requires :key, type: String end @@ -226,11 +264,11 @@ expect(JSON.parse(last_response.body)).to eq('items' => []) end it 'adds to declared parameters' do subject.params do - requires :items do + requires :items, type: Array do requires :key end end expect(subject.route_setting(:declared_params)).to eq([items: [:key]]) end @@ -270,22 +308,22 @@ expect(last_response.body).to eq('required works') end it 'adds to declared parameters' do subject.params do - requires :items do + requires :items, type: Array do requires :key end end expect(subject.route_setting(:declared_params)).to eq([items: [:key]]) end end context 'group' do before do subject.params do - group :items do + group :items, type: Array do requires :key end end subject.get '/required' do 'required works' @@ -304,11 +342,11 @@ expect(last_response.body).to eq('required works') end it 'adds to declared parameters' do subject.params do - group :items do + group :items, type: Array do requires :key end end expect(subject.route_setting(:declared_params)).to eq([items: [:key]]) end @@ -317,11 +355,11 @@ context 'group params with nested params which has a type' do let(:invalid_items){ { items: '' } } before do subject.params do - optional :items do + optional :items, type: Array do optional :key1, type: String optional :key2, type: String end end subject.post '/group_with_nested' do @@ -393,13 +431,13 @@ end context 'validation within arrays' do before do subject.params do - group :children do + group :children, type: Array do requires :name - group :parents do + group :parents, type: Array do requires :name end end end subject.get '/within_array' do @@ -455,11 +493,11 @@ end context 'with block param' do before do subject.params do - requires :planets do + requires :planets, type: Array do requires :name end end subject.get '/req' do 'within array works' @@ -467,11 +505,11 @@ subject.put '/req' do '' end subject.params do - group :stars do + group :stars, type: Array do requires :name end end subject.get '/grp' do 'within array works' @@ -480,11 +518,11 @@ '' end subject.params do requires :name - optional :moons do + optional :moons, type: Array do requires :name end end subject.get '/opt' do 'within array works' @@ -547,13 +585,13 @@ end context 'validation within arrays with JSON' do before do subject.params do - group :children do + group :children, type: Array do requires :name - group :parents do + group :parents, type: Array do requires :name end end end subject.put '/within_array' do @@ -628,11 +666,11 @@ expect(last_response.body).to eq('items is invalid') end it 'adds to declared parameters' do subject.params do - optional :items do + optional :items, type: Array do requires :key end end expect(subject.route_setting(:declared_params)).to eq([items: [:key]]) end @@ -690,13 +728,13 @@ expect(last_response.body).to eq('items[optional_subitems][value] is missing') end it 'adds to declared parameters' do subject.params do - optional :items do + optional :items, type: Array do requires :key - optional(:optional_subitems) { requires :value } - requires(:required_subitems) { requires :value } + optional(:optional_subitems, type: Array) { requires :value } + requires(:required_subitems, type: Array) { requires :value } end end expect(subject.route_setting(:declared_params)).to eq([items: [:key, { optional_subitems: [:value] }, { required_subitems: [:value] }]]) end end