Skip to content

Commit

Permalink
fix: update validation for creating pacticipants to return a 400 when…
Browse files Browse the repository at this point in the history
… name is missing
  • Loading branch information
bethesque committed Jun 15, 2022
1 parent e1bee35 commit fcb02aa
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 2 deletions.
28 changes: 28 additions & 0 deletions lib/pact_broker/api/contracts/pacticipant_create_schema.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
require "pact_broker/api/contracts/pacticipant_schema"

module PactBroker
module Api
module Contracts
class PacticipantCreateSchema
extend DryValidationWorkarounds
extend PactBroker::Messages
using PactBroker::HashRefinements

SCHEMA = Dry::Validation.Schema do
configure do
predicates(DryValidationPredicates)
config.messages_file = File.expand_path("../../../locale/en.yml", __FILE__)
end
required(:name).filled(:str?, :single_line?)
end

def self.call(params_with_string_keys)
params = params_with_string_keys&.symbolize_keys
update_errors = PacticipantSchema::SCHEMA.call(params).messages(full: true)
create_errors = SCHEMA.call(params).messages(full: true)
select_first_message(flatten_indexed_messages(update_errors.merge(create_errors)))
end
end
end
end
end
4 changes: 2 additions & 2 deletions lib/pact_broker/api/resources/pacticipants.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
require "pact_broker/api/decorators/pacticipant_decorator"
require "pact_broker/domain/pacticipant"
require "pact_broker/hash_refinements"
require "pact_broker/api/contracts/pacticipant_schema"
require "pact_broker/api/contracts/pacticipant_create_schema"

module PactBroker
module Api
Expand Down Expand Up @@ -66,7 +66,7 @@ def policy_name
private

def schema
PactBroker::Api::Contracts::PacticipantSchema
PactBroker::Api::Contracts::PacticipantCreateSchema
end

def pacticipants
Expand Down
6 changes: 6 additions & 0 deletions spec/features/create_pacticipant_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,10 @@
subject
expect(response_body).to include pacticipant_hash
end

context "with an empty JSON document" do
let(:pacticipant_hash) { {} }

its(:status) { is_expected.to eq 400 }
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
require "pact_broker/api/contracts/pacticipant_schema"

module PactBroker
module Api
module Contracts
describe PacticipantCreateSchema do
let(:params) do
{
name: "pact-broker",
displayName: "Pact Broker",
mainBranch: main_branch,
repositoryUrl: "https://github.com/pact-foundation/pact_broker",
repositoryName: "pact_broker",
repositoryNamespace: "pact-foundation"
}
end

let(:main_branch) { "main" }

subject { PacticipantCreateSchema.call(params) }

context "with valid params" do
it { is_expected.to be_empty }
end

context "with empty params" do
let(:params) do
{
repositoryUrl: "",
repositoryName: "",
repositoryNamespace: ""
}
end

its([:name, 0]) { is_expected.to include "name is missing" }
end
end
end
end
end

0 comments on commit fcb02aa

Please sign in to comment.