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

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

Constant Summary collapse

YAML_WHITELISTED_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?, #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
32
33
# 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.concat(
        [
          '**/*.eyaml',
          '**/*.eyml',
        ],
      )
    else
      pat
    end
  end
end

#spinner_textObject



35
36
37
38
39
# File 'lib/pdk/validate/yaml/yaml_syntax_validator.rb', line 35

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

#validate_target(report, target) ⇒ Object



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
83
84
85
86
87
88
89
90
91
# File 'lib/pdk/validate/yaml/yaml_syntax_validator.rb', line 41

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), YAML_WHITELISTED_CLASSES, [], true)

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