Class: SBOM::CycloneDX::Validator::BaseValidator
- Inherits:
-
Object
- Object
- SBOM::CycloneDX::Validator::BaseValidator
show all
- Defined in:
- lib/sbom/cyclone_dx/validator/base_validator.rb,
sig/sbom/cyclone_dx/validator/base_validator.rbs
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
#const? ⇒ Boolean
16
|
# File 'sig/sbom/cyclone_dx/validator/base_validator.rbs', line 16
def const?: () -> bool
|
#raw_types ⇒ Array[fieldType]
37
38
39
|
# File 'lib/sbom/cyclone_dx/validator/base_validator.rb', line 37
def raw_types
@types
end
|
#required? ⇒ Boolean
33
34
35
|
# File 'lib/sbom/cyclone_dx/validator/base_validator.rb', line 33
def required?
@required
end
|
#valid?(value) ⇒ Boolean
18
19
20
|
# File 'lib/sbom/cyclone_dx/validator/base_validator.rb', line 18
def valid?(value)
validate(value).empty?
end
|
#validate(value) ⇒ Array[String]
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)
errors = []
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
|