Sha256: 3d2a38f621043d332f5d1fff6b8a3f415bff0db7af0dd5cd3c3126c2c22eb54b

Contents?: true

Size: 1.79 KB

Versions: 3

Compression:

Stored size: 1.79 KB

Contents

require "pact_broker/error"

module PactBroker
  module Api
    module Contracts
      module DryValidationErrorsFormatter

        # Formats the dry validation errors in the expected PactBroker error format of { :key => ["errors"] }
        # where there are no nested hashes.
        # @param [Dry::Validation::MessageSet] errors
        # @return [Hash]
        def format_errors(errors)
          errors.each_with_object({}) do | error, errors_hash |
            integers = error.path.select{ | k | k.is_a?(Integer) }

            if integers.size > 1
              raise PactBroker::Error,  "Cannot currently format an error message with more than one index"
            end

            if integers.empty?
              key = error.path == [nil] ? nil : error.path.join(".").to_sym
              add_error(errors_hash, key, error.text)
            else
              add_indexed_error(errors_hash, error)
            end
          end
        end

        # @private
        def add_error(errors_hash, key, text)
          errors_hash[key] ||= []
          errors_hash[key] << text
        end

        # @private
        def add_indexed_error(errors_hash, error)
          error_path_classes = error.path.collect(&:class)
          if error_path_classes == [Symbol, Integer, Symbol]
            add_error(errors_hash, error.path.first, "#{error.path.last} #{error.text} (at index #{error.path[1]})")
          elsif error_path_classes == [Symbol, Integer]
            add_error(errors_hash, error.path.first, "#{error.text} (at index #{error.path[1]})")
          else
            # Don't have any usecases for this - will deal with it when it happens
            raise PactBroker::Error, "Cannot currently format an error message with path classes #{error_path_classes}"
          end
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
pact_broker-2.107.1 lib/pact_broker/api/contracts/dry_validation_errors_formatter.rb
pact_broker-2.107.0 lib/pact_broker/api/contracts/dry_validation_errors_formatter.rb
pact_broker-2.107.0.beta.1 lib/pact_broker/api/contracts/dry_validation_errors_formatter.rb