Module: Oselvar::Var::Config

Defined in:
lib/oselvar/var/config.rb

Overview

Strict, fail-loud reader for the shared var.config.json format. Missing file → empty config; malformed JSON, wrong types, or unknown keys → an error starting with the file path. See conformance/config/README.md.

Defined Under Namespace

Classes: VarConfig

Constant Summary collapse

VERSION =
'0.3.2'
KNOWN_KEYS =
%w[$schema docs steps snippets scannerPlugins].freeze
KNOWN_DOCS_KEYS =
%w[include exclude].freeze

Class Method Summary collapse

Class Method Details

.read_var_config(root) ⇒ Object

Raises:

  • (ArgumentError)


25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/oselvar/var/config.rb', line 25

def read_var_config(root)
  path = File.join(root.to_s, 'var.config.json')
  return VarConfig.new unless File.file?(path)

  data = begin
    JSON.parse(File.read(path, encoding: 'UTF-8'))
  rescue JSON::ParserError => e
    raise ArgumentError, "#{path}: invalid JSON: #{e.message}"
  end
  raise ArgumentError, "#{path}: top level must be an object" unless data.is_a?(::Hash)

  unknown = data.keys - KNOWN_KEYS
  raise ArgumentError, "#{path}: unknown key(s): #{unknown.sort.join(', ')}" unless unknown.empty?

  docs = data['docs'] || {}
  raise ArgumentError, "#{path}: 'docs' must be an object" unless docs.is_a?(::Hash)

  unknown_docs = docs.keys - KNOWN_DOCS_KEYS
  raise ArgumentError, "#{path}: unknown docs key(s): #{unknown_docs.sort.join(', ')}" unless unknown_docs.empty?

  snippets = data['snippets'] || {}
  unless snippets.is_a?(::Hash) && snippets.all? { |k, v| k.is_a?(String) && v.is_a?(String) }
    raise ArgumentError, "#{path}: 'snippets' must be an object of strings"
  end

  VarConfig.new(
    docs_include: string_array(docs['include'], 'docs.include', path),
    docs_exclude: string_array(docs['exclude'], 'docs.exclude', path),
    steps: string_array(data['steps'], 'steps', path),
    snippets: snippets,
    scanner_plugins: string_array(data['scannerPlugins'], 'scannerPlugins', path)
  )
end

.string_array(value, key, path) ⇒ Object



59
60
61
62
63
64
65
66
# File 'lib/oselvar/var/config.rb', line 59

def string_array(value, key, path)
  return [] if value.nil?
  unless value.is_a?(Array) && value.all?(String)
    raise ArgumentError, "#{path}: '#{key}' must be an array of strings"
  end

  value
end