Sha256: 2003a1564568dbfffed97c7b3bd6751b8155cf4c8fa439a07bb60b389e4a6b20

Contents?: true

Size: 1.73 KB

Versions: 40

Compression:

Stored size: 1.73 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[:name] }

        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
    end
  end
end

Version data entries

40 entries across 40 versions & 1 rubygems

Version Path
karafka-2.2.14 lib/karafka/contracts/consumer_group.rb
karafka-2.2.13 lib/karafka/contracts/consumer_group.rb
karafka-2.2.12 lib/karafka/contracts/consumer_group.rb
karafka-2.2.11 lib/karafka/contracts/consumer_group.rb
karafka-2.2.10 lib/karafka/contracts/consumer_group.rb
karafka-2.2.9 lib/karafka/contracts/consumer_group.rb
karafka-2.2.8 lib/karafka/contracts/consumer_group.rb
karafka-2.2.8.beta1 lib/karafka/contracts/consumer_group.rb
karafka-2.2.7 lib/karafka/contracts/consumer_group.rb
karafka-2.2.6 lib/karafka/contracts/consumer_group.rb
karafka-2.2.5 lib/karafka/contracts/consumer_group.rb
karafka-2.2.4 lib/karafka/contracts/consumer_group.rb
karafka-2.2.3 lib/karafka/contracts/consumer_group.rb
karafka-2.2.2 lib/karafka/contracts/consumer_group.rb
karafka-2.2.1 lib/karafka/contracts/consumer_group.rb
karafka-2.2.0 lib/karafka/contracts/consumer_group.rb
karafka-2.1.13 lib/karafka/contracts/consumer_group.rb
karafka-2.1.12 lib/karafka/contracts/consumer_group.rb
karafka-2.1.11 lib/karafka/contracts/consumer_group.rb
karafka-2.1.10 lib/karafka/contracts/consumer_group.rb