Module: Perfgate::Config::Validator

Defined in:
lib/perfgate/config/validator.rb

Overview

Validates a merged configuration hash against Schema::TREE and a handful of range/type rules that matter for Milestone 1 (sample and warmup counts). Raises Perfgate::ConfigurationError on any problem, per spec section 11 ("unknown keys fail validation").

Constant Summary collapse

MINIMUM_SAMPLES =
3

Class Method Summary collapse

Class Method Details

.call(hash) ⇒ Object



14
15
16
17
18
19
# File 'lib/perfgate/config/validator.rb', line 14

def call(hash)
  check_unknown_keys(hash, Schema::TREE, [])
  check_version(hash)
  check_execution(hash)
  hash
end

.check_execution(hash) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/perfgate/config/validator.rb', line 49

def check_execution(hash)
  samples = hash.dig(:execution, :samples)
  warmup = hash.dig(:execution, :warmup)

  if samples && (!samples.is_a?(Integer) || samples < MINIMUM_SAMPLES)
    raise Perfgate::ConfigurationError,
          "execution.samples must be an integer >= #{MINIMUM_SAMPLES}, got #{samples.inspect}"
  end

  return unless warmup && (!warmup.is_a?(Integer) || warmup.negative?)

  raise Perfgate::ConfigurationError, "execution.warmup must be a non-negative integer, got #{warmup.inspect}"
end

.check_known_key(key, schema, path) ⇒ Object



37
38
39
40
41
# File 'lib/perfgate/config/validator.rb', line 37

def check_known_key(key, schema, path)
  return if schema.key?(key)

  raise Perfgate::ConfigurationError, "unknown configuration key: #{(path + [key]).join(".")}"
end

.check_unknown_keys(hash, schema, path) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/perfgate/config/validator.rb', line 21

def check_unknown_keys(hash, schema, path)
  hash.each_key do |key|
    check_known_key(key, schema, path)

    nested_schema = schema[key]
    next unless nested_schema.is_a?(Hash)

    value = hash[key]
    unless value.is_a?(Hash)
      raise Perfgate::ConfigurationError, "expected #{(path + [key]).join(".")} to be a mapping"
    end

    check_unknown_keys(value, nested_schema, path + [key])
  end
end

.check_version(hash) ⇒ Object



43
44
45
46
47
# File 'lib/perfgate/config/validator.rb', line 43

def check_version(hash)
  return if hash[:version] == 1

  raise Perfgate::ConfigurationError, "unsupported configuration version: #{hash[:version].inspect} (expected 1)"
end