Sha256: 62808098932795925a36f581a2764e887be4af785428cfa3a816822335c79114

Contents?: true

Size: 1.61 KB

Versions: 17

Compression:

Stored size: 1.61 KB

Contents

# frozen_string_literal: true

module WaterDrop
  module Contracts
    # Contract with validation rules for WaterDrop configuration details
    class Config < ::Karafka::Core::Contractable::Contract
      configure do |config|
        config.error_messages = YAML.safe_load(
          File.read(
            File.join(WaterDrop.gem_root, 'config', 'locales', 'errors.yml')
          )
        ).fetch('en').fetch('validations').fetch('config')
      end

      required(:id) { |val| val.is_a?(String) && !val.empty? }
      required(:logger) { |val| !val.nil? }
      required(:deliver) { |val| [true, false].include?(val) }
      required(:max_payload_size) { |val| val.is_a?(Integer) && val >= 1 }
      required(:max_wait_timeout) { |val| val.is_a?(Numeric) && val >= 0 }
      required(:wait_timeout) { |val| val.is_a?(Numeric) && val.positive? }
      required(:kafka) { |val| val.is_a?(Hash) && !val.empty? }
      required(:wait_on_queue_full) { |val| [true, false].include?(val) }
      required(:wait_backoff_on_queue_full) { |val| val.is_a?(Numeric) && val >= 0 }
      required(:wait_timeout_on_queue_full) { |val| val.is_a?(Numeric) && val >= 0 }

      # rdkafka allows both symbols and strings as keys for config but then casts them to strings
      # This can be confusing, so we expect all keys to be symbolized
      virtual do |config, errors|
        next true unless errors.empty?

        errors = []

        config
          .fetch(:kafka)
          .keys
          .reject { |key| key.is_a?(Symbol) }
          .each { |key| errors << [[:kafka, key], :kafka_key_must_be_a_symbol] }

        errors
      end
    end
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
waterdrop-2.7.0.alpha1 lib/waterdrop/contracts/config.rb
waterdrop-2.6.14 lib/waterdrop/contracts/config.rb
waterdrop-2.6.13 lib/waterdrop/contracts/config.rb
waterdrop-2.6.12 lib/waterdrop/contracts/config.rb
waterdrop-2.6.11 lib/waterdrop/contracts/config.rb
waterdrop-2.6.10 lib/waterdrop/contracts/config.rb
waterdrop-2.6.9 lib/waterdrop/contracts/config.rb
waterdrop-2.6.8 lib/waterdrop/contracts/config.rb
waterdrop-2.6.7 lib/waterdrop/contracts/config.rb
waterdrop-2.6.6 lib/waterdrop/contracts/config.rb
waterdrop-2.6.5 lib/waterdrop/contracts/config.rb
waterdrop-2.6.4 lib/waterdrop/contracts/config.rb
waterdrop-2.6.3 lib/waterdrop/contracts/config.rb
waterdrop-2.6.2 lib/waterdrop/contracts/config.rb
waterdrop-2.6.1 lib/waterdrop/contracts/config.rb
waterdrop-2.6.1.beta1 lib/waterdrop/contracts/config.rb
waterdrop-2.6.0 lib/waterdrop/contracts/config.rb