Module: DependencyManager::ConfigSchemaMacros

Included in:
Factory
Defined in:
lib/dependency_manager/config_schema_macros.rb

Overview

Class-level methods for validation of configurations

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ void

This method returns an undefined value.

Hook for binding class-level methods to the child class

Parameters:

  • klass (Class)

    Factory to bind to



12
13
14
# File 'lib/dependency_manager/config_schema_macros.rb', line 12

def self.included(klass)
  klass.extend(ClassMethods)
end

Instance Method Details

#validate(target: configuration) ⇒ Dry::Validation::Result

Runs validation against configuration without throwing errors

Parameters:

  • target: (defaults to: configuration)

    configuration [Hash[Symbol, Any]] Configuration to validate, defaulting to ‘configuration`

Returns:

  • (Dry::Validation::Result)


22
23
24
# File 'lib/dependency_manager/config_schema_macros.rb', line 22

def validate(target: configuration)
  self.class.validate(**target)
end

#validate!(target: configuration) ⇒ TrueClass

Immediate return validation that will raise an exception if the contract is not fulfilled

Parameters:

  • target: (defaults to: configuration)

    configuration [Hash[Symbol, Any]] Configuration to validate, defaulting to ‘configuration`

Returns:

  • (TrueClass)

    Success

Raises:

  • (ArgumentError)


37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/dependency_manager/config_schema_macros.rb', line 37

def validate!(target: configuration)
  validation_result = validate(target: target)

  return true if validation_result.success?

  errors = validation_result
    .errors
    .map { |e| "#{e.path} #{e.text}" }
    .join(', ')

  raise ArgumentError, "Configuration is invalid: #{errors}"
end