Class: AbideDevUtils::Sce::Validate::Strings::BaseValidator

Inherits:
Object
  • Object
show all
Defined in:
lib/abide_dev_utils/sce/validate/strings/base_validator.rb

Overview

Base class for validating Puppet Strings objects. This class can be used directly, but it is recommended to use a subclass of this class to provide more specific validation logic. Each subclass should implement a ‘validate_<type>` method that will be called by the `validate` method of this class. The `validate_<type>` method should contain the validation logic for the corresponding type of Puppet Strings object.

Direct Known Subclasses

PuppetClassValidator

Constant Summary collapse

SAFE_OBJECT_METHODS =
%i[
  type
  name
  file
  line
  docstring
  tags
  parameters
  source
].freeze
PDK_SUMMARY_REGEX =
%r{^A short summary of the purpose.*}.freeze
PDK_DESCRIPTION_REGEX =
%r{^A description of what this.*}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(strings_object) ⇒ BaseValidator

Returns a new instance of BaseValidator.



30
31
32
33
34
35
36
37
# File 'lib/abide_dev_utils/sce/validate/strings/base_validator.rb', line 30

def initialize(strings_object)
  @object = strings_object
  @findings = []
  # Define instance methods for each of the SAFE_OBJECT_METHODS
  SAFE_OBJECT_METHODS.each do |method|
    define_singleton_method(method) { safe_method_call(@object, method) }
  end
end

Instance Attribute Details

#findingsObject (readonly)

Returns the value of attribute findings.



28
29
30
# File 'lib/abide_dev_utils/sce/validate/strings/base_validator.rb', line 28

def findings
  @findings
end

Instance Method Details

#errorsObject



39
40
41
# File 'lib/abide_dev_utils/sce/validate/strings/base_validator.rb', line 39

def errors
  @findings.select { |f| f.type == :error }
end

#errors?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/abide_dev_utils/sce/validate/strings/base_validator.rb', line 47

def errors?
  !errors.empty?
end

#find_parameter(param_name) ⇒ Object



65
66
67
# File 'lib/abide_dev_utils/sce/validate/strings/base_validator.rb', line 65

def find_parameter(param_name)
  parameters&.find { |p| p[0] == param_name }
end

#find_tag_name(tag_name) ⇒ Object



55
56
57
# File 'lib/abide_dev_utils/sce/validate/strings/base_validator.rb', line 55

def find_tag_name(tag_name)
  tags&.find { |t| t.tag_name == tag_name }
end

#license_tag?Boolean

Checks if the object has a license tag and if it is formatted correctly. Comparison is not case sensitive.

Returns:

  • (Boolean)


78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/abide_dev_utils/sce/validate/strings/base_validator.rb', line 78

def license_tag?
  see_tags = select_tag_name('see')
  if see_tags.empty? || see_tags.none? { |t| t.name.casecmp('LICENSE.pdf') && t.text.casecmp('for license') }
    new_finding(
      :error,
      :no_license_tag,
      remediation: 'Add "@see LICENSE.pdf for license" to the class documentation'
    )
    return false
  end
  true
end

#non_generic_description?Boolean

Checks if the description is not the default PDK description.

Returns:

  • (Boolean)


104
105
106
107
108
109
110
111
112
113
# File 'lib/abide_dev_utils/sce/validate/strings/base_validator.rb', line 104

def non_generic_description?
  description = docstring
  return true if description.nil?

  if description.match?(PDK_DESCRIPTION_REGEX)
    new_finding(:warning, :generic_description, remediation: 'Add a more descriptive description')
    return false
  end
  true
end

#non_generic_summary?Boolean

Checks if the summary is not the default PDK summary.

Returns:

  • (Boolean)


92
93
94
95
96
97
98
99
100
101
# File 'lib/abide_dev_utils/sce/validate/strings/base_validator.rb', line 92

def non_generic_summary?
  summary = find_tag_name('summary')&.text
  return true if summary.nil?

  if summary.match?(PDK_SUMMARY_REGEX)
    new_finding(:warning, :generic_summary, remediation: 'Add a more descriptive summary')
    return false
  end
  true
end

#select_tag_name(tag_name) ⇒ Object



59
60
61
62
63
# File 'lib/abide_dev_utils/sce/validate/strings/base_validator.rb', line 59

def select_tag_name(tag_name)
  return [] if tags.nil?

  tags.select { |t| t.tag_name == tag_name }
end

#validateObject



69
70
71
72
73
74
# File 'lib/abide_dev_utils/sce/validate/strings/base_validator.rb', line 69

def validate
  license_tag?
  non_generic_summary?
  non_generic_description?
  send("validate_#{type}".to_sym) if respond_to?("validate_#{type}".to_sym)
end

#warningsObject



43
44
45
# File 'lib/abide_dev_utils/sce/validate/strings/base_validator.rb', line 43

def warnings
  @findings.select { |f| f.type == :warning }
end

#warnings?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/abide_dev_utils/sce/validate/strings/base_validator.rb', line 51

def warnings?
  !warnings.empty?
end