Class: PactBroker::Api::Contracts::VerifiablePactsJSONQuerySchema

Inherits:
Object
  • Object
show all
Extended by:
DryValidationWorkarounds
Defined in:
lib/pact_broker/api/contracts/verifiable_pacts_json_query_schema.rb

Constant Summary collapse

SCHEMA =
Dry::Validation.Schema do
  configure do
    predicates(DryValidationPredicates)
    config.messages_file = File.expand_path("../../../locale/en.yml", __FILE__)
  end
  optional(:providerVersionTags).maybe(:array?)
  optional(:consumerVersionSelectors).each do
    schema do
      # configure do
      #   def self.messages
      #     super.merge(en: { errors: { fallbackTagMustBeForLatest: 'can only be set if latest=true' }})
      #   end
      # end

      required(:tag).filled(:str?)
      optional(:latest).filled(included_in?: [true, false])
      optional(:fallbackTag).filled(:str?)
      optional(:consumer).filled(:str?, :not_blank?)

      # rule(fallbackTagMustBeForLatest: [:fallbackTag, :latest]) do | fallback_tag, latest |
      #   fallback_tag.filled?.then(latest.eql?(true))
      # end
    end
  end
  optional(:includePendingStatus).filled(included_in?: [true, false])
  optional(:includeWipPactsSince).filled(:date?)
end

Class Method Summary collapse

Methods included from DryValidationWorkarounds

flatten_array_of_hashes, flatten_indexed_messages, is_indexed_structure?, select_first_message

Class Method Details

.add_cross_field_validation_errors(params, results) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/pact_broker/api/contracts/verifiable_pacts_json_query_schema.rb', line 48

def self.add_cross_field_validation_errors(params, results)
  # This is a ducking joke. Need to get rid of dry-validation
  if params[:consumerVersionSelectors].is_a?(Array)
    errors = []
    params[:consumerVersionSelectors].each_with_index do | selector, index |
      if selector[:fallbackTag] && !selector[:latest]
        errors << "fallbackTag can only be set if latest is true (at index #{index})"
      end

      if selector[:consumer] && selector[:latest]
        errors << "specifying a consumer with latest == true is not yet supported (at index #{index})"
      end
    end
    if errors.any?
      results[:consumerVersionSelectors] ||= []
      results[:consumerVersionSelectors].concat(errors)
    end
  end
end

.call(params) ⇒ Object



41
42
43
44
45
46
# File 'lib/pact_broker/api/contracts/verifiable_pacts_json_query_schema.rb', line 41

def self.call(params)
  symbolized_params = params&.symbolize_keys
  results = select_first_message(flatten_indexed_messages(SCHEMA.call(symbolized_params).messages(full: true)))
  add_cross_field_validation_errors(symbolized_params, results)
  results
end