From b26ddb4626405aeaf3171e4f2588d0630497d264 Mon Sep 17 00:00:00 2001 From: Beth Skurrie Date: Thu, 27 Jul 2023 09:43:36 +1000 Subject: [PATCH] fix: gracefully handle validating an array when a hash is expected --- .../api/contracts/base_contract.rb | 3 ++- .../api/contracts/base_contract_spec.rb | 26 +++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 spec/lib/pact_broker/api/contracts/base_contract_spec.rb diff --git a/lib/pact_broker/api/contracts/base_contract.rb b/lib/pact_broker/api/contracts/base_contract.rb index 945fcbc9f..c8537a9e1 100644 --- a/lib/pact_broker/api/contracts/base_contract.rb +++ b/lib/pact_broker/api/contracts/base_contract.rb @@ -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 diff --git a/spec/lib/pact_broker/api/contracts/base_contract_spec.rb b/spec/lib/pact_broker/api/contracts/base_contract_spec.rb new file mode 100644 index 000000000..19e77bdfe --- /dev/null +++ b/spec/lib/pact_broker/api/contracts/base_contract_spec.rb @@ -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