Class: PDK::Validate::YAML::YAMLSyntaxValidator

Inherits:
InternalRubyValidator show all
Defined in:
lib/pdk/validate/yaml/yaml_syntax_validator.rb

Constant Summary collapse

YAML_ALLOWLISTED_CLASSES =
[Symbol].freeze

Instance Attribute Summary

Attributes inherited from Validator

#context, #options, #prepared

Instance Method Summary collapse

Methods inherited from InternalRubyValidator

#before_validation, #invoke, #prepare_invoke!

Methods inherited from InvokableValidator

#allow_empty_targets?, #fnmatch?, #ignore_dotfiles?, #invoke_style, #parse_targets, #pattern_ignore, #prepare_invoke!, #process_invalid, #process_skipped, #spinner, #valid_in_context?

Methods inherited from Validator

#initialize, #invoke, #prepare_invoke!, #spinner, #spinners_enabled?, #start_spinner, #stop_spinner, #valid_in_context?

Constructor Details

This class inherits a constructor from PDK::Validate::Validator

Instance Method Details

#ignore_dotfilesObject



9
10
11
# File 'lib/pdk/validate/yaml/yaml_syntax_validator.rb', line 9

def ignore_dotfiles
  false
end

#nameObject



13
14
15
# File 'lib/pdk/validate/yaml/yaml_syntax_validator.rb', line 13

def name
  'yaml-syntax'
end

#patternObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/pdk/validate/yaml/yaml_syntax_validator.rb', line 17

def pattern
  [
    '**/*.yaml',
    '**/*.yml'
  ].tap do |pat|
    if context.is_a?(PDK::Context::ControlRepo)
      pat.push(
        '**/*.eyaml',
        '**/*.eyml'
      )
    else
      pat
    end
  end
end

#spinner_textObject



33
34
35
# File 'lib/pdk/validate/yaml/yaml_syntax_validator.rb', line 33

def spinner_text
  format('Checking YAML syntax (%{patterns}).', patterns: pattern.join(' '))
end

#validate_target(report, target) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/pdk/validate/yaml/yaml_syntax_validator.rb', line 37

def validate_target(report, target)
  return 0 unless PDK::Util::Filesystem.file?(target)

  unless PDK::Util::Filesystem.readable?(target)
    report.add_event(
      file: target,
      source: name,
      state: :failure,
      severity: 'error',
      message: 'Could not be read.'
    )
    return 1
  end

  begin
    ::YAML.safe_load(PDK::Util::Filesystem.read_file(target), permitted_classes: YAML_ALLOWLISTED_CLASSES, permitted_symbols: [], aliases: true)

    report.add_event(
      file: target,
      source: name,
      state: :passed,
      severity: 'ok'
    )
    0
  rescue Psych::SyntaxError => e
    report.add_event(
      file: target,
      source: name,
      state: :failure,
      severity: 'error',
      line: e.line,
      column: e.column,
      message: format('%{problem} %{context}', problem: e.problem, context: e.context)
    )
    1
  rescue Psych::DisallowedClass => e
    report.add_event(
      file: target,
      source: name,
      state: :failure,
      severity: 'error',
      message: format('Unsupported class: %{message}', message: e.message)
    )
    1
  end
end