-
-
Notifications
You must be signed in to change notification settings - Fork 177
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: update validation for creating pacticipants to return a 400 when…
… name is missing
- Loading branch information
Showing
4 changed files
with
76 additions
and
2 deletions.
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
lib/pact_broker/api/contracts/pacticipant_create_schema.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
spec/lib/pact_broker/api/contracts/pacticipant_create_schema_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |