Skip to content

Commit

Permalink
Format nested schema errors
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Blease <[email protected]>
  • Loading branch information
Alex Blease authored and Pivotal committed Jun 19, 2017
1 parent 9541b96 commit 64ee0a2
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 10 deletions.
18 changes: 15 additions & 3 deletions lib/services/service_brokers/validation_errors_formatter.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module VCAP::Services::ServiceBrokers
class ValidationErrorsFormatter
INDENT = ' '.freeze

def format(validation_errors)
message = "\n"
validation_errors.messages.each { |e| message += "#{e}\n" }
Expand All @@ -9,19 +10,30 @@ def format(validation_errors)

message += "Service #{service.name}\n"
service_errors.messages.each do |error|
message += "#{INDENT}#{error}\n"
message += indent + "#{error}\n"
end

service_errors.nested_errors.each do |plan, plan_errors|
next if plan_errors.empty?

message += "#{INDENT}Plan #{plan.name}\n"
message += indent + "Plan #{plan.name}\n"
plan_errors.messages.each do |error|
message += "#{INDENT}#{INDENT}#{error}\n"
message += indent(2) + "#{error}\n"
end

plan_errors.nested_errors.each do |schema, schema_errors|
message += indent(2) + "Schemas\n"
schema_errors.messages.each do |error|
message += indent(3) + "#{error}\n"
end
end
end
end
message
end

def indent(num=1)
INDENT.to_s * num
end
end
end
13 changes: 13 additions & 0 deletions spec/acceptance/broker_api_compatibility/broker_api_v2.13_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,18 @@
expect(parsed_body['entity']['schemas']).to eq({ 'service_instance' => { 'create' => { 'parameters' => {} } } })
end
end

context 'when the broker catalog has an invalid schema' do
before do
update_broker(default_catalog(plan_schemas: { 'service_instance' => { 'create' => true } }))
end

it 'returns an error' do
parsed_body = MultiJson.load(last_response.body)

expect(parsed_body['code']).to eq(270012)
expect(parsed_body['description']).to include('Schemas service_instance.create must be a hash, but has value true')
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,26 @@ module VCAP::Services::ServiceBrokers
let(:service_broker) { instance_double(VCAP::CloudController::ServiceBroker) }
let(:service_1) { V2::CatalogService.new(service_broker, 'name' => 'service-1') }
let(:service_2) { V2::CatalogService.new(service_broker, 'name' => 'service-2') }
let(:plan_123) { V2::CatalogPlan.new(service_2, 'name' => 'plan-123') }
let(:plan_1) { V2::CatalogPlan.new(service_2, 'name' => 'plan-1') }
let(:service_3) { V2::CatalogService.new(service_broker, 'name' => 'service-3') }
let(:service_4) { V2::CatalogService.new(service_broker, 'name' => 'service-4') }
let(:plan_2) { V2::CatalogPlan.new(service_2, 'name' => 'plan-2') }
let(:schemas) { V2::CatalogSchemas.new('schemas' => {}) }

before do
errors.add('Service ids must be unique')
errors.add_nested(service_1).
add('Service id must be a string, but has value 123')
add('Service id must be a string, but has value foo')
errors.add_nested(service_2).
add('Plan ids must be unique').
add_nested(plan_123).
add('Plan name must be a string, but has value 123')
add_nested(plan_1).
add('Plan name must be a string, but has value 1')
errors.add_nested(service_3).
add('At least one plan is required')
errors.add_nested(service_4).
add_nested(plan_2).
add_nested(schemas).
add('Schemas must be valid')
end

it 'builds a formatted string' do
Expand All @@ -29,13 +36,17 @@ module VCAP::Services::ServiceBrokers
Service ids must be unique
Service service-1
Service id must be a string, but has value 123
Service id must be a string, but has value foo
Service service-2
Plan ids must be unique
Plan plan-123
Plan name must be a string, but has value 123
Plan plan-1
Plan name must be a string, but has value 1
Service service-3
At least one plan is required
Service service-4
Plan plan-2
Schemas
Schemas must be valid
HEREDOC
end
end
Expand Down

0 comments on commit 64ee0a2

Please sign in to comment.