spec/grape/validations_spec.rb in grape-0.16.2 vs spec/grape/validations_spec.rb in grape-0.17.0

- old
+ new

@@ -243,11 +243,11 @@ end it 'errors when param is not an Array' do get '/required', items: 'hello' expect(last_response.status).to eq(400) - expect(last_response.body).to eq('items is invalid, items[key] is missing') + expect(last_response.body).to eq('items is invalid') get '/required', items: { key: 'foo' } expect(last_response.status).to eq(400) expect(last_response.body).to eq('items is invalid') end @@ -335,11 +335,11 @@ expect(last_response.status).to eq(400) expect(last_response.body).to eq('items is invalid, items[key] is missing, items[key] is invalid') get '/required', items: [{ key: 'hash in array' }] expect(last_response.status).to eq(400) - expect(last_response.body).to eq('items is invalid, items[0][key] does not have a valid value') + expect(last_response.body).to eq('items is invalid, items[key] does not have a valid value') end it 'works when all params match' do get '/required', items: { key: 'a' } expect(last_response.status).to eq(200) @@ -394,11 +394,11 @@ subject.post '/group_with_nested' do 'group with nested works' end end - it 'errors when group param is invalid'do + it 'errors when group param is invalid' do post '/group_with_nested', items: invalid_items expect(last_response.status).to eq(400) end end @@ -406,11 +406,11 @@ module ValuesSpec module DateRangeValidations class DateRangeValidator < Grape::Validations::Base def validate_param!(attr_name, params) return if params[attr_name][:from] <= params[attr_name][:to] - fail Grape::Exceptions::Validation, params: [@scope.full_name(attr_name)], message: "'from' must be lower or equal to 'to'" + raise Grape::Exceptions::Validation, params: [@scope.full_name(attr_name)], message: "'from' must be lower or equal to 'to'" end end end end @@ -464,11 +464,11 @@ before do subject.params do group :children, type: Array do requires :name group :parents, type: Array do - requires :name + requires :name, allow_blank: false end end end subject.get '/within_array' do 'within array works' @@ -484,19 +484,39 @@ expect(last_response.body).to eq('within array works') end it 'errors when a parameter is not present' do get '/within_array', children: [ - { name: 'Jim', parents: [{}] }, - { name: 'Job', parents: [{ name: 'Joy' }] } + { name: 'Jim', parents: [{ name: 'Joy' }] }, + { name: 'Job', parents: [{}] } ] # NOTE: with body parameters in json or XML or similar this # should actually fail with: children[parents][name] is missing. expect(last_response.status).to eq(400) - expect(last_response.body).to eq('children[0][parents] is missing') + expect(last_response.body).to eq('children[1][parents] is missing') end + it 'errors when a parameter is not present in array within array' do + get '/within_array', children: [ + { name: 'Jim', parents: [{ name: 'Joy' }] }, + { name: 'Job', parents: [{ name: 'Bill' }, { name: '' }] } + ] + + expect(last_response.status).to eq(400) + expect(last_response.body).to eq('children[1][parents][1][name] is empty') + end + + it 'handle errors for all array elements' do + get '/within_array', children: [ + { name: 'Jim', parents: [] }, + { name: 'Job', parents: [] } + ] + + expect(last_response.status).to eq(400) + expect(last_response.body).to eq('children[0][parents] is missing, children[1][parents] is missing') + end + it 'safely handles empty arrays and blank parameters' do # NOTE: with body parameters in json or XML or similar this # should actually return 200, since an empty array is valid. get '/within_array', children: [] expect(last_response.status).to eq(400) @@ -505,18 +525,17 @@ expect(last_response.status).to eq(400) expect(last_response.body).to eq('children[0][parents] is missing') end it 'errors when param is not an Array' do - # NOTE: would be nicer if these just returned 'children is invalid' get '/within_array', children: 'hello' expect(last_response.status).to eq(400) - expect(last_response.body).to eq('children is invalid, children[name] is missing, children[parents] is missing, children[parents] is invalid, children[parents][name] is missing') + expect(last_response.body).to eq('children is invalid') get '/within_array', children: { name: 'foo' } expect(last_response.status).to eq(400) - expect(last_response.body).to eq('children is invalid, children[parents] is missing') + expect(last_response.body).to eq('children is invalid') get '/within_array', children: [name: 'Jay', parents: { name: 'Fred' }] expect(last_response.status).to eq(400) expect(last_response.body).to eq('children[0][parents] is invalid') end @@ -563,11 +582,11 @@ end it 'requires defaults to Array type' do get '/req', planets: 'Jupiter, Saturn' expect(last_response.status).to eq(400) - expect(last_response.body).to eq('planets is invalid, planets[name] is missing') + expect(last_response.body).to eq('planets is invalid') get '/req', planets: { name: 'Jupiter' } expect(last_response.status).to eq(400) expect(last_response.body).to eq('planets is invalid') @@ -579,11 +598,11 @@ end it 'optional defaults to Array type' do get '/opt', name: 'Jupiter', moons: 'Europa, Ganymede' expect(last_response.status).to eq(400) - expect(last_response.body).to eq('moons is invalid, moons[name] is missing') + expect(last_response.body).to eq('moons is invalid') get '/opt', name: 'Jupiter', moons: { name: 'Ganymede' } expect(last_response.status).to eq(400) expect(last_response.body).to eq('moons is invalid') @@ -598,11 +617,11 @@ end it 'group defaults to Array type' do get '/grp', stars: 'Sun' expect(last_response.status).to eq(400) - expect(last_response.body).to eq('stars is invalid, stars[name] is missing') + expect(last_response.body).to eq('stars is invalid') get '/grp', stars: { name: 'Sun' } expect(last_response.status).to eq(400) expect(last_response.body).to eq('stars is invalid') @@ -687,11 +706,11 @@ end it "errors when param is present but isn't an Array" do get '/optional_group', items: 'hello' expect(last_response.status).to eq(400) - expect(last_response.body).to eq('items is invalid, items[key] is missing') + expect(last_response.body).to eq('items is invalid') get '/optional_group', items: { key: 'foo' } expect(last_response.status).to eq(400) expect(last_response.body).to eq('items is invalid') end @@ -792,11 +811,11 @@ context 'custom validation' do module CustomValidations class Customvalidator < Grape::Validations::Base def validate_param!(attr_name, params) return if params[attr_name] == 'im custom' - fail Grape::Exceptions::Validation, params: [@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 context 'when using optional with a custom validator' do @@ -940,10 +959,10 @@ context 'when using options on param' do module CustomValidations class CustomvalidatorWithOptions < Grape::Validations::Base def validate_param!(attr_name, params) return if params[attr_name] == @option[:text] - fail Grape::Exceptions::Validation, params: [@scope.full_name(attr_name)], message: message + raise Grape::Exceptions::Validation, params: [@scope.full_name(attr_name)], message: message end end end before do