Sha256: b71a4d68bebacdc30c50668a3ad07227bcd00ed6efb0285f0998d5ef9c46e910

Contents?: true

Size: 1.13 KB

Versions: 5

Compression:

Stored size: 1.13 KB

Contents

module Overcommit
  # Validates and normalizes a configuration.
  class ConfigurationValidator
    # Validates hash for any invalid options, normalizing where possible.
    def validate(hash)
      hash = convert_nils_to_empty_hashes(hash)
      ensure_hook_type_sections_exist(hash)

      hash
    end

    private

    # Ensures that keys for all supported hook types exist (PreCommit,
    # CommitMsg, etc.)
    def ensure_hook_type_sections_exist(hash)
      Overcommit::Utils.supported_hook_type_classes.each do |hook_type|
        hash[hook_type] ||= {}
        hash[hook_type]['ALL'] ||= {}
      end
    end

    # Normalizes `nil` values to empty hashes.
    #
    # This is useful for when we want to merge two configuration hashes
    # together, since it's easier to merge two hashes than to have to check if
    # one of the values is nil.
    def convert_nils_to_empty_hashes(hash)
      hash.each_with_object({}) do |(key, value), h|
        h[key] =
          case value
          when nil  then {}
          when Hash then convert_nils_to_empty_hashes(value)
          else
            value
          end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 2 rubygems

Version Path
overcommit-0.23.0 lib/overcommit/configuration_validator.rb
overcommit-0.22.0 lib/overcommit/configuration_validator.rb
jawshooah-overcommit-0.22.0 lib/overcommit/configuration_validator.rb
overcommit-0.21.0 lib/overcommit/configuration_validator.rb
overcommit-0.20.0 lib/overcommit/configuration_validator.rb