Class: PDK::Validate::InternalRubyValidator Abstract

Inherits:
InvokableValidator show all
Defined in:
lib/pdk/validate/internal_ruby_validator.rb

Overview

This class is abstract.

An abstract validator that runs ruby code internal to the PDK e.g. JSON and YAML validation, on a single file. The validator code must run within the PDK Ruby environment as opposed to the bundled Ruby environment for a module.

At a a minimum child classes should implment the ‘name`, `pattern` and `validate_target` methods

An example concrete implementation could look like:

module PDK

module Validate
  module Tasks
    class TasksNameValidator < InternalRubyValidator
      def name
        'task-name'
      end

      def pattern
        'tasks/**/*'
      end

      def validate_target(report, target)
        task_name = File.basename(target, File.extname(target))
  ... ruby code ...
        success ? 0 : 1
      end
    end
  end
end

end

See Also:

Instance Attribute Summary

Attributes inherited from Validator

#context, #options, #prepared

Instance Method Summary collapse

Methods inherited from InvokableValidator

#allow_empty_targets?, #fnmatch?, #ignore_dotfiles?, #invoke_style, #parse_targets, #pattern, #pattern_ignore, #process_invalid, #process_skipped, #spinner, #spinner_text, #valid_in_context?

Methods inherited from Validator

#initialize, #name, #spinner, #spinner_text, #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

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method is abstract.

Tasks to run before validation occurs. This is run once every time ‘.invoke` is called



98
# File 'lib/pdk/validate/internal_ruby_validator.rb', line 98

def before_validation; end

#invoke(report) ⇒ Object

Invokes the validator to call ‘validate_target` on each target

See Also:

  • Validator.invoke


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/internal_ruby_validator.rb', line 52

def invoke(report)
  prepare_invoke!

  process_skipped(report, @skipped)
  process_invalid(report, @invalid)

  return 0 if @targets.empty?

  return_val = 0

  before_validation

  start_spinner
  @targets.each do |target|
    validation_result = validate_target(report, target)
    if validation_result.nil?
      report.add_event(
        file: target,
        source: name,
        state: :failure,
        severity: 'error',
        message: "Validation did not return an exit code for #{target}"
      )
      validation_result = 1
    end
    return_val = validation_result if validation_result > return_val
  end

  stop_spinner(return_val.zero?)
  return_val
end

#prepare_invoke!Object

See Also:

  • Validator.prepare_invoke!


39
40
41
42
43
44
45
46
47
48
# File 'lib/pdk/validate/internal_ruby_validator.rb', line 39

def prepare_invoke!
  return if @prepared

  super

  # Parse the targets
  @targets, @skipped, @invalid = parse_targets

  nil
end

#validate_target(report, target) ⇒ Integer

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method is abstract.

Validates a single target It is the responsibility of this method to populate the report with validation messages

Parameters:

  • report (PDK::Report)

    The report to add the events to

  • target (String)

    The target to validate

Returns:

  • (Integer)

    The exitcode of the validation. Zero indicates success. A non-zero code indicates failure



93
# File 'lib/pdk/validate/internal_ruby_validator.rb', line 93

def validate_target(report, target); end