Class: Packwerk::Layer::Validator

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Includes:
Validator
Defined in:
lib/packwerk/layer/validator.rb

Constant Summary collapse

Result =
Packwerk::Validator::Result

Instance Method Summary collapse

Instance Method Details

#call(package_set, configuration) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/packwerk/layer/validator.rb', line 13

def call(package_set, configuration)
  results = T.let([], T::Array[Result])

  package_set.each do |package|
    config = package.config
    f = Pathname.new(package.name).join('package.yml').to_s
    package = Package.from(package, layers)

    next if !config

    result = check_enforce_key(package, f, config)
    results << result
    next if !result.ok?

    result = check_enforce_layers_setting(f, config[layer_config.enforce_key])
    results << result
    next if !result.ok?

    result = check_layer_setting(package, f)
    results << result
    next if !result.ok?
  end

  merge_results(results, separator: "\n---\n")
end

#check_enforce_key(package, config_file_path, config) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/packwerk/layer/validator.rb', line 57

def check_enforce_key(package, config_file_path, config)
  enforce_layer_present = !config[Config::LAYER_ENFORCE].nil?
  enforce_architecture_present = !config[Config::ARCHITECTURE_ENFORCE].nil?

  if layer_config.enforce_key == Config::LAYER_ENFORCE && enforce_architecture_present
    Result.new(
      ok: false,
      error_value: "Unexpected `enforce_architecture` option in #{config_file_path.inspect}. Did you mean `enforce_layers`?"
    )
  elsif layer_config.enforce_key == Config::ARCHITECTURE_ENFORCE && enforce_layer_present
    Result.new(
      ok: false,
      error_value: "Unexpected `enforce_layers` option in #{config_file_path.inspect}. Did you mean `enforce_architecture`?"
    )
  else
    Result.new(ok: true)
  end
end

#check_enforce_layers_setting(config_file_path, setting) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/packwerk/layer/validator.rb', line 101

def check_enforce_layers_setting(config_file_path, setting)
  activated_value = [true, 'strict'].include?(setting)
  valid_value = [true, nil, false, 'strict'].include?(setting)
  layers_set = layers.names.any?
  if !valid_value
    Result.new(
      ok: false,
      error_value: "Invalid '#{layer_config.enforce_key}' option in #{config_file_path.inspect}: #{setting.inspect}"
    )
  elsif activated_value && !layers_set
    Result.new(
      ok: false,
      error_value: "Cannot set '#{layer_config.enforce_key}' option in #{config_file_path.inspect} until `layers` have been specified in `packwerk.yml`"
    )
  else
    Result.new(ok: true)
  end
end

#check_layer_setting(package, config_file_path) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/packwerk/layer/validator.rb', line 79

def check_layer_setting(package, config_file_path)
  layer = package.layer
  valid_layer = layer.nil? || layers.names.include?(layer)

  if layer.nil? && package.enforces?
    Result.new(
      ok: false,
      error_value: "Invalid 'layer' option in #{config_file_path.inspect}: #{package.layer.inspect}. `layer` must be set if `#{layer_config.enforce_key}` is on."
    )
  elsif valid_layer
    Result.new(ok: true)
  else
    Result.new(
      ok: false,
      error_value: "Invalid 'layer' option in #{config_file_path.inspect}: #{layer.inspect}. Must be one of #{layers.names_list.inspect}"
    )
  end
end

#layer_configObject



45
46
47
# File 'lib/packwerk/layer/validator.rb', line 45

def layer_config
  @layer_config ||= T.let(Config.new, T.nilable(Config))
end

#layersObject



40
41
42
# File 'lib/packwerk/layer/validator.rb', line 40

def layers
  @layers ||= T.let(Layers.new, T.nilable(Packwerk::Layer::Layers))
end

#permitted_keysObject



50
51
52
# File 'lib/packwerk/layer/validator.rb', line 50

def permitted_keys
  [layer_config.enforce_key, 'layer']
end