spec/grape/validations_spec.rb in grape-1.2.4 vs spec/grape/validations_spec.rb in grape-1.2.5
- old
+ new
@@ -538,19 +538,29 @@
{ 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')
+ expect(last_response.body).to eq(
+ 'children[0][parents][0][name] is missing, ' \
+ 'children[1][parents][0][name] 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)
- expect(last_response.body).to eq('children is missing')
+ expect(last_response.body).to eq(
+ 'children[0][name] is missing, ' \
+ 'children[0][parents] is missing, ' \
+ 'children[0][parents] is invalid, ' \
+ 'children[0][parents][0][name] is missing, ' \
+ 'children[0][parents][0][name] is empty'
+ )
+
get '/within_array', children: [name: 'Jay']
expect(last_response.status).to eq(400)
expect(last_response.body).to eq('children[0][parents] is missing')
end
@@ -1221,11 +1231,13 @@
'mutually_exclusive works!'
end
end
get '/custom_message/mutually_exclusive', beer: 'true', wine: 'true', nested: { scotch: 'true', aquavit: 'true' }, nested2: [{ scotch2: 'true' }, { scotch2: 'true', aquavit2: 'true' }]
expect(last_response.status).to eq(400)
- expect(last_response.body).to eq 'beer, wine are mutually exclusive pass only one, scotch, aquavit are mutually exclusive pass only one, scotch2, aquavit2 are mutually exclusive pass only one'
+ expect(last_response.body).to eq(
+ 'beer, wine are mutually exclusive pass only one, nested[scotch], nested[aquavit] are mutually exclusive pass only one, nested2[1][scotch2], nested2[1][aquavit2] are mutually exclusive pass only one'
+ )
end
end
it 'errors for all sets' do
subject.params do
@@ -1247,11 +1259,11 @@
'mutually_exclusive works!'
end
get '/mutually_exclusive', beer: 'true', wine: 'true', nested: { scotch: 'true', aquavit: 'true' }, nested2: [{ scotch2: 'true' }, { scotch2: 'true', aquavit2: 'true' }]
expect(last_response.status).to eq(400)
- expect(last_response.body).to eq 'beer, wine are mutually exclusive, scotch, aquavit are mutually exclusive, scotch2, aquavit2 are mutually exclusive'
+ expect(last_response.body).to eq 'beer, wine are mutually exclusive, nested[scotch], nested[aquavit] are mutually exclusive, nested2[1][scotch2], nested2[1][aquavit2] are mutually exclusive'
end
end
context 'in a group' do
it 'works when only one from the set is present' do
@@ -1316,11 +1328,11 @@
subject.resources :custom_message do
params do
optional :beer
optional :wine
optional :juice
- exactly_one_of :beer, :wine, :juice, message: { exactly_one: 'are missing, exactly one parameter is required', mutual_exclusion: 'are mutually exclusive, exactly one parameter is required' }
+ exactly_one_of :beer, :wine, :juice, message: 'are missing, exactly one parameter is required'
end
get '/exactly_one_of' do
'exactly_one_of works!'
end
end
@@ -1350,11 +1362,11 @@
end
it 'errors when two or more are present' do
get '/custom_message/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, exactly one parameter is required'
+ expect(last_response.body).to eq 'beer, wine, juice are missing, exactly one parameter is required'
end
end
it 'errors when none are present' do
get '/exactly_one_of'
@@ -1369,11 +1381,11 @@
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, juice are missing, exactly one parameter must be provided'
end
end
context 'nested params' do
before :each do
@@ -1397,11 +1409,11 @@
end
it 'errors when none are present' do
get '/exactly_one_of_nested'
expect(last_response.status).to eq(400)
- expect(last_response.body).to eq 'nested is missing, beer_nested, wine_nested, juice_nested are missing, exactly one parameter must be provided'
+ expect(last_response.body).to eq 'nested is missing, nested[beer_nested], nested[wine_nested], nested[juice_nested] are missing, exactly one parameter must be provided'
end
it 'succeeds when one is present' do
get '/exactly_one_of_nested', nested: { beer_nested: 'string' }
expect(last_response.status).to eq(200)
@@ -1409,11 +1421,11 @@
end
it 'errors when two or more are present' do
get '/exactly_one_of_nested', nested: { beer_nested: 'string' }, nested2: [{ beer_nested2: 'string', wine_nested2: 'anotherstring' }]
expect(last_response.status).to eq(400)
- expect(last_response.body).to eq 'beer_nested2, wine_nested2 are mutually exclusive'
+ expect(last_response.body).to eq 'nested2[0][beer_nested2], nested2[0][wine_nested2], nested2[0][juice_nested2] are missing, exactly one parameter must be provided'
end
end
end
context 'at least one of' do
@@ -1483,40 +1495,40 @@
context 'nested params' do
before :each do
subject.params do
requires :nested, type: Hash do
- optional :beer_nested
- optional :wine_nested
- optional :juice_nested
- at_least_one_of :beer_nested, :wine_nested, :juice_nested
+ optional :beer
+ optional :wine
+ optional :juice
+ at_least_one_of :beer, :wine, :juice
end
optional :nested2, type: Array do
- optional :beer_nested2
- optional :wine_nested2
- optional :juice_nested2
- at_least_one_of :beer_nested2, :wine_nested2, :juice_nested2
+ optional :beer
+ optional :wine
+ optional :juice
+ at_least_one_of :beer, :wine, :juice
end
end
subject.get '/at_least_one_of_nested' do
'at_least_one_of works!'
end
end
it 'errors when none are present' do
get '/at_least_one_of_nested'
expect(last_response.status).to eq(400)
- expect(last_response.body).to eq 'nested is missing, beer_nested, wine_nested, juice_nested are missing, at least one parameter must be provided'
+ expect(last_response.body).to eq 'nested is missing, nested[beer], nested[wine], nested[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_nested', nested: { beer_nested: 'string' }, nested2: [{ beer_nested2: 'string' }]
+ get '/at_least_one_of_nested', nested: { beer: 'string' }, nested2: [{ 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_nested', nested: { beer_nested: 'string', wine_nested: 'string' }, nested2: [{ beer_nested2: 'string', wine_nested2: 'string' }]
+ get '/at_least_one_of_nested', nested: { beer: 'string', wine: 'string' }, nested2: [{ 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