Sha256: fe7b30f58904b75baf6b1337fe8e66a0bc06c0379a6e690ed6dc6fe6a44a904e

Contents?: true

Size: 1.54 KB

Versions: 10

Compression:

Stored size: 1.54 KB

Contents

# frozen_string_literal: true

module Karafka
  module Contracts
    # Consumer group topic validation rules.
    class ConsumerGroupTopic < Base
      configure do |config|
        config.error_messages = YAML.safe_load(
          File.read(
            File.join(Karafka.gem_root, 'config', 'errors.yml')
          )
        ).fetch('en').fetch('validations').fetch('consumer_group_topic')
      end

      required(:consumer) { |val| !val.nil? }
      required(:deserializer) { |val| !val.nil? }
      required(:id) { |val| val.is_a?(String) && Contracts::TOPIC_REGEXP.match?(val) }
      required(:kafka) { |val| val.is_a?(Hash) && !val.empty? }
      required(:max_messages) { |val| val.is_a?(Integer) && val >= 1 }
      required(:initial_offset) { |val| %w[earliest latest].include?(val) }
      required(:max_wait_time) { |val| val.is_a?(Integer) && val >= 10 }
      required(:manual_offset_management) { |val| [true, false].include?(val) }
      required(:name) { |val| val.is_a?(String) && Contracts::TOPIC_REGEXP.match?(val) }
      required(:subscription_group) { |val| val.nil? || (val.is_a?(String) && !val.empty?) }

      virtual do |data, errors|
        next unless errors.empty?

        value = data.fetch(:kafka)

        begin
          # This will trigger rdkafka validations that we catch and re-map the info and use dry
          # compatible format
          Rdkafka::Config.new(value).send(:native_config)

          nil
        rescue Rdkafka::Config::ConfigError => e
          [[%w[kafka], e.message]]
        end
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
karafka-2.0.15 lib/karafka/contracts/consumer_group_topic.rb
karafka-2.0.14 lib/karafka/contracts/consumer_group_topic.rb
karafka-2.0.13 lib/karafka/contracts/consumer_group_topic.rb
karafka-2.0.12 lib/karafka/contracts/consumer_group_topic.rb
karafka-2.0.11 lib/karafka/contracts/consumer_group_topic.rb
karafka-2.0.10 lib/karafka/contracts/consumer_group_topic.rb
karafka-2.0.9 lib/karafka/contracts/consumer_group_topic.rb
karafka-2.0.8 lib/karafka/contracts/consumer_group_topic.rb
karafka-2.0.7 lib/karafka/contracts/consumer_group_topic.rb
karafka-2.0.6 lib/karafka/contracts/consumer_group_topic.rb