Class: PDK::Validate::Metadata::MetadataSyntaxValidator

Inherits:
InternalRubyValidator show all
Defined in:
lib/pdk/validate/metadata/metadata_syntax_validator.rb

Instance Attribute Summary

Attributes inherited from Validator

#context, #options, #prepared

Instance Method Summary collapse

Methods inherited from InternalRubyValidator

#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, #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

#before_validationObject



25
26
27
28
29
30
31
32
# File 'lib/pdk/validate/metadata/metadata_syntax_validator.rb', line 25

def before_validation
  # The pure ruby JSON parser gives much nicer parse error messages than
  # the C extension at the cost of slightly slower parsing. We require it
  # here and restore the C extension at the end of the method (if it was
  # being used).
  require 'json/pure'
  JSON.parser = JSON::Pure::Parser
end

#invoke(report) ⇒ Object



19
20
21
22
23
# File 'lib/pdk/validate/metadata/metadata_syntax_validator.rb', line 19

def invoke(report)
  super
ensure
  JSON.parser = JSON::Ext::Parser if defined?(JSON::Ext::Parser)
end

#nameObject



7
8
9
# File 'lib/pdk/validate/metadata/metadata_syntax_validator.rb', line 7

def name
  'metadata-syntax'
end

#patternObject



11
12
13
# File 'lib/pdk/validate/metadata/metadata_syntax_validator.rb', line 11

def pattern
  contextual_pattern(['metadata.json', 'tasks/*.json'])
end

#spinner_textObject



15
16
17
# File 'lib/pdk/validate/metadata/metadata_syntax_validator.rb', line 15

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

#validate_target(report, target) ⇒ Object



34
35
36
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
# File 'lib/pdk/validate/metadata/metadata_syntax_validator.rb', line 34

def validate_target(report, 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
    JSON.parse(PDK::Util::Filesystem.read_file(target))

    report.add_event(
      file: target,
      source: name,
      state: :passed,
      severity: 'ok'
    )
    0
  rescue JSON::ParserError => e
    # Because the message contains a raw segment of the file, we use
    # String#dump here to unescape any escape characters like newlines.
    # We then strip out the surrounding quotes and the exclaimation
    # point that json_pure likes to put in exception messages.
    sane_message = e.message.dump[/\A"(.+?)!?"\Z/, 1]

    report.add_event(
      file: target,
      source: name,
      state: :failure,
      severity: 'error',
      message: sane_message
    )
    1
  end
end