Skip to content

Commit

Permalink
fix: gracefully handle validating an array when a hash is expected
Browse files Browse the repository at this point in the history
  • Loading branch information
bethesque committed Jul 26, 2023
1 parent 43ad63b commit b26ddb4
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/pact_broker/api/contracts/base_contract.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ class BaseContract < Dry::Validation::Contract
# @param [Hash] the parameters to validate
# @return [Hash] the validation errors to display to the user
def self.call(params)
format_errors(new.call(params&.symbolize_keys).errors)
params_to_validate = params.respond_to?(:symbolize_keys) ? params.symbolize_keys : params
format_errors(new.call(params_to_validate).errors)
end
end
end
Expand Down
26 changes: 26 additions & 0 deletions spec/lib/pact_broker/api/contracts/base_contract_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
require "pact_broker/api/contracts/base_contract"

module PactBroker
module Api
module Contracts
describe BaseContract do

class TestContract < BaseContract
json do
required(:name).filled(:string)
end
end

describe ".call" do
context "when an array is supplied" do
subject { TestContract.call([1]) }

it "doesn't blow up" do
expect(subject[:name]).to eq ["is missing"]
end
end
end
end
end
end
end

0 comments on commit b26ddb4

Please sign in to comment.