Class: SBOM::CycloneDX::Validator::BaseValidator

Inherits:
Object
  • Object
show all
Defined in:
lib/sbom/cyclone_dx/validator/base_validator.rb

Constant Summary collapse

MISSING_REQUIRED =
"Required property is missing or nil"
INVALID_TYPE =
"Invalid type `%s`, expected: %s"

Instance Method Summary collapse

Constructor Details

#initialize(*types, required: false) ⇒ BaseValidator

Returns a new instance of BaseValidator.



11
12
13
14
15
16
# File 'lib/sbom/cyclone_dx/validator/base_validator.rb', line 11

def initialize(*types, required: false)
  raise "Abstract class BaseValidator cannot be instantiated" unless self.class < BaseValidator

  @types = types
  @required = required
end

Instance Method Details

#raw_typesObject



37
38
39
# File 'lib/sbom/cyclone_dx/validator/base_validator.rb', line 37

def raw_types
  @types
end

#required?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/sbom/cyclone_dx/validator/base_validator.rb', line 33

def required?
  @required
end

#valid?(value) ⇒ Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/sbom/cyclone_dx/validator/base_validator.rb', line 18

def valid?(value)
  validate(value).empty?
end

#validate(value) ⇒ Object

rubocop:disable Metrics/CyclomaticComplexity



22
23
24
25
26
27
28
29
30
31
# File 'lib/sbom/cyclone_dx/validator/base_validator.rb', line 22

def validate(value) # rubocop:disable Metrics/CyclomaticComplexity
  errors = [] #: Array[String]

  errors << MISSING_REQUIRED if required? && value.nil?
  return errors if errors.any?
  return errors if @types.any? { |type| value.is_a?(type) }
  return errors if value.nil? && !required?

  errors << format(INVALID_TYPE, value.class.name, @types.join(", "))
end