Sha256: 32de03a00cd3a0675874f6563e7a880b5dd887894c7cc052c2ae235c787eaea9

Contents?: true

Size: 1.95 KB

Versions: 19

Compression:

Stored size: 1.95 KB

Contents

# frozen_string_literal: true

module Karafka
  module Contracts
    # Contract for single full route (consumer group + topics) validation.
    class ConsumerGroup < Base
      configure do |config|
        config.error_messages = YAML.safe_load(
          File.read(
            File.join(Karafka.gem_root, 'config', 'locales', 'errors.yml')
          )
        ).fetch('en').fetch('validations').fetch('consumer_group')
      end

      required(:id) { |val| val.is_a?(String) && Contracts::TOPIC_REGEXP.match?(val) }
      required(:topics) { |val| val.is_a?(Array) && !val.empty? }

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

        names = data.fetch(:topics).map { |topic| topic_unique_key(topic) }

        next if names.size == names.uniq.size

        [[%i[topics], :names_not_unique]]
      end

      virtual do |data, errors|
        next unless errors.empty?
        next unless ::Karafka::App.config.strict_topics_namespacing

        names = data.fetch(:topics).map { |topic| topic[:name] }
        names_hash = names.each_with_object({}) { |n, h| h[n] = true }
        error_occured = false
        names.each do |n|
          # Skip topic names that are not namespaced
          next unless n.chars.find { |c| ['.', '_'].include?(c) }

          if n.chars.include?('.')
            # Check underscore styled topic
            underscored_topic = n.tr('.', '_')
            error_occured = names_hash[underscored_topic] ? true : false
          else
            # Check dot styled topic
            dot_topic = n.tr('_', '.')
            error_occured = names_hash[dot_topic] ? true : false
          end
        end

        next unless error_occured

        [[%i[topics], :topics_namespaced_names_not_unique]]
      end

      class << self
        # @param topic [Hash] topic config hash
        # @return [String] topic unique key for validators
        def topic_unique_key(topic)
          topic[:name]
        end
      end
    end
  end
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
karafka-2.4.9 lib/karafka/contracts/consumer_group.rb
karafka-2.4.8 lib/karafka/contracts/consumer_group.rb
karafka-2.4.7 lib/karafka/contracts/consumer_group.rb
karafka-2.4.6 lib/karafka/contracts/consumer_group.rb
karafka-2.4.5 lib/karafka/contracts/consumer_group.rb
karafka-2.4.4 lib/karafka/contracts/consumer_group.rb
karafka-2.4.3 lib/karafka/contracts/consumer_group.rb
karafka-2.4.0 lib/karafka/contracts/consumer_group.rb
karafka-2.4.0.rc1 lib/karafka/contracts/consumer_group.rb
karafka-2.3.4 lib/karafka/contracts/consumer_group.rb
karafka-2.4.0.beta2 lib/karafka/contracts/consumer_group.rb
karafka-2.4.0.beta1 lib/karafka/contracts/consumer_group.rb
karafka-2.3.3 lib/karafka/contracts/consumer_group.rb
karafka-2.3.2 lib/karafka/contracts/consumer_group.rb
karafka-2.3.1 lib/karafka/contracts/consumer_group.rb
karafka-2.3.0 lib/karafka/contracts/consumer_group.rb
karafka-2.3.0.rc1 lib/karafka/contracts/consumer_group.rb
karafka-2.3.0.alpha2 lib/karafka/contracts/consumer_group.rb
karafka-2.3.0.alpha1 lib/karafka/contracts/consumer_group.rb