Module: Philiprehberger::ConfigValidator

Defined in:
lib/philiprehberger/config_validator.rb,
lib/philiprehberger/config_validator/rule.rb,
lib/philiprehberger/config_validator/schema.rb,
lib/philiprehberger/config_validator/version.rb

Defined Under Namespace

Classes: Error, Rule, Schema, ValidationError

Constant Summary collapse

VERSION =
'0.5.0'

Class Method Summary collapse

Class Method Details

.define {|Schema| ... } ⇒ Schema

Define a configuration schema using a DSL block

Yields:

  • (Schema)

    the schema instance for DSL evaluation

Returns:

  • (Schema)

    the defined schema

Raises:

  • (Error)

    if no block is given



19
20
21
22
23
24
25
# File 'lib/philiprehberger/config_validator.rb', line 19

def self.define(&block)
  raise Error, 'a block is required' unless block

  schema = Schema.new
  schema.instance_eval(&block)
  schema
end

.validate(config) {|Schema| ... } ⇒ Array<String>

Validate a configuration hash against a schema defined in a block

Parameters:

  • config (Hash)

    the configuration to validate

Yields:

  • (Schema)

    the schema definition block

Returns:

  • (Array<String>)

    validation error messages



32
33
34
35
# File 'lib/philiprehberger/config_validator.rb', line 32

def self.validate(config, &)
  schema = define(&)
  schema.validate(config)
end

.validate!(config) {|Schema| ... } ⇒ Hash

Validate a configuration hash and raise on errors

Parameters:

  • config (Hash)

    the configuration to validate

Yields:

  • (Schema)

    the schema definition block

Returns:

  • (Hash)

    the validated configuration with defaults applied

Raises:



43
44
45
46
# File 'lib/philiprehberger/config_validator.rb', line 43

def self.validate!(config, &)
  schema = define(&)
  schema.validate!(config)
end