Sha256: a070ab7fb32b7d3de85326162eb490d899c2edc309796cb1888e0efa24514a81

Contents?: true

Size: 1.12 KB

Versions: 5

Compression:

Stored size: 1.12 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.inject({}) do |h, (key, value)|
        h[key] =
          case value
          when nil  then {}
          when Hash then convert_nils_to_empty_hashes(value)
          else
            value
          end
        h
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
overcommit-0.17.0 lib/overcommit/configuration_validator.rb
overcommit-0.16.0 lib/overcommit/configuration_validator.rb
overcommit-0.15.0 lib/overcommit/configuration_validator.rb
overcommit-0.14.1 lib/overcommit/configuration_validator.rb
overcommit-0.14.0 lib/overcommit/configuration_validator.rb