Class: PDK::Validate::Tasks::TasksMetadataLintValidator

Inherits:
InternalRubyValidator show all
Defined in:
lib/pdk/validate/tasks/tasks_metadata_lint_validator.rb

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

#nameObject



7
8
9
# File 'lib/pdk/validate/tasks/tasks_metadata_lint_validator.rb', line 7

def name
  'task-metadata-lint'
end

#patternObject



11
12
13
# File 'lib/pdk/validate/tasks/tasks_metadata_lint_validator.rb', line 11

def pattern
  contextual_pattern('tasks/*.json')
end

#schema_fileObject



19
20
21
22
23
24
25
26
27
28
# File 'lib/pdk/validate/tasks/tasks_metadata_lint_validator.rb', line 19

def schema_file
  require 'pdk/config'

  path = PDK::Config.json_schema('task')
  schema = PDK::Util::Filesystem.read_file(path)

  JSON.parse(schema)
rescue JSON::ParserError
  raise PDK::CLI::FatalError, 'Failed to parse Task Metadata Schema file.'
end

#spinner_textObject



15
16
17
# File 'lib/pdk/validate/tasks/tasks_metadata_lint_validator.rb', line 15

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

#validate_target(report, target) ⇒ Object



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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/pdk/validate/tasks/tasks_metadata_lint_validator.rb', line 30

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

  require 'json-schema'
  begin
    # Need to set the JSON Parser and State Generator to the Native one to be
    # compatible with the multi_json adapter.
    JSON.parser = JSON::Ext::Parser if defined?(JSON::Ext::Parser)
    JSON.generator = JSON::Ext::Generator if defined?(JSON::Ext::Generator)

    begin
      errors = JSON::Validator.fully_validate(schema_file, PDK::Util::Filesystem.read_file(target)) || []
    rescue JSON::Schema::SchemaError => e
      raise PDK::CLI::FatalError, format('Unable to validate Task Metadata. %{error}.', error: e.message)
    end

    if errors.empty?
      report.add_event(
        file: target,
        source: name,
        state: :passed,
        severity: 'ok'
      )
      0
    else
      errors.each do |error|
        # strip off the trailing parts that aren't relevant
        error = error.split('in schema').first if error.include? 'in schema'

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