Class: Philiprehberger::ConfigValidator::Rule

Inherits:
Object
  • Object
show all
Defined in:
lib/philiprehberger/config_validator/rule.rb

Overview

Represents a single validation rule for a configuration key

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key, type, required:, default: nil, one_of: nil) ⇒ Rule

Returns a new instance of Rule.

Parameters:

  • key (Symbol)

    the configuration key

  • type (Class)

    the expected type

  • required (Boolean)

    whether the key is required

  • default (Object, nil) (defaults to: nil)

    the default value for optional keys

  • one_of (Array, nil) (defaults to: nil)

    allowed values constraint



27
28
29
30
31
32
33
# File 'lib/philiprehberger/config_validator/rule.rb', line 27

def initialize(key, type, required:, default: nil, one_of: nil)
  @key = key
  @type = type
  @required = required
  @default = default
  @allowed_values = one_of
end

Instance Attribute Details

#allowed_valuesArray? (readonly)

Returns allowed values.

Returns:

  • (Array, nil)

    allowed values



20
21
22
# File 'lib/philiprehberger/config_validator/rule.rb', line 20

def allowed_values
  @allowed_values
end

#defaultObject? (readonly)

Returns the default value.

Returns:

  • (Object, nil)

    the default value



17
18
19
# File 'lib/philiprehberger/config_validator/rule.rb', line 17

def default
  @default
end

#keySymbol (readonly)

Returns the configuration key.

Returns:

  • (Symbol)

    the configuration key



8
9
10
# File 'lib/philiprehberger/config_validator/rule.rb', line 8

def key
  @key
end

#requiredBoolean (readonly)

Returns whether the key is required.

Returns:

  • (Boolean)

    whether the key is required



14
15
16
# File 'lib/philiprehberger/config_validator/rule.rb', line 14

def required
  @required
end

#typeClass (readonly)

Returns the expected type.

Returns:

  • (Class)

    the expected type



11
12
13
# File 'lib/philiprehberger/config_validator/rule.rb', line 11

def type
  @type
end

Instance Method Details

#apply_default(config) ⇒ void

This method returns an undefined value.

Apply default value to config if key is missing

Parameters:

  • config (Hash)

    the configuration hash



58
59
60
61
62
63
# File 'lib/philiprehberger/config_validator/rule.rb', line 58

def apply_default(config)
  return if config.key?(key) || config.key?(key.to_s)
  return if default.nil?

  config[key] = default
end

#validate(config) ⇒ Array<String>

Validate a configuration hash against this rule

Parameters:

  • config (Hash)

    the configuration to validate

Returns:

  • (Array<String>)

    validation error messages



39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/philiprehberger/config_validator/rule.rb', line 39

def validate(config)
  errors = []
  value = resolve_value(config)

  if value.nil?
    errors << "missing required key '#{key}'" if required
    return errors
  end

  validate_type(value, errors)
  validate_allowed(value, errors)

  errors
end