Class: Sbom::Validator

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

Constant Summary collapse

SPDX_VERSIONS =
%w[2.2 2.3].freeze
CYCLONEDX_VERSIONS =
%w[1.4 1.5 1.6 1.7].freeze
EXTENSION_MAP =
{
  ".spdx" => :spdx,
  ".spdx.json" => :spdx,
  ".spdx.yaml" => :spdx,
  ".spdx.yml" => :spdx,
  ".spdx.xml" => :spdx,
  ".spdx.rdf" => :spdx,
  ".cdx.json" => :cyclonedx,
  ".bom.json" => :cyclonedx,
  ".cdx.xml" => :cyclonedx,
  ".bom.xml" => :cyclonedx
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sbom_type: :auto, version: nil, schema_dir: nil) ⇒ Validator

Returns a new instance of Validator.



24
25
26
27
28
# File 'lib/sbom/validator.rb', line 24

def initialize(sbom_type: :auto, version: nil, schema_dir: nil)
  @sbom_type = sbom_type
  @version = version
  @schema_dir = schema_dir || default_schema_dir
end

Class Method Details

.validate_file(filename, sbom_type: :auto) ⇒ Object



59
60
61
# File 'lib/sbom/validator.rb', line 59

def self.validate_file(filename, sbom_type: :auto)
  new(sbom_type: sbom_type).validate_file(filename)
end

.validate_file!(filename, sbom_type: :auto) ⇒ Object



63
64
65
# File 'lib/sbom/validator.rb', line 63

def self.validate_file!(filename, sbom_type: :auto)
  new(sbom_type: sbom_type).validate_file!(filename)
end

Instance Method Details

#validate_file(filename) ⇒ Object

Raises:



30
31
32
33
34
35
36
37
38
# File 'lib/sbom/validator.rb', line 30

def validate_file(filename)
  raise ValidatorError, "File not found: #{filename}" unless File.exist?(filename)
  raise ValidatorError, "Empty file: #{filename}" if File.size(filename).zero?

  content = File.read(filename)
  sbom_type = detect_type(filename, content)

  validate_content(content, sbom_type)
end

#validate_file!(filename) ⇒ Object

Raises:



40
41
42
43
44
45
# File 'lib/sbom/validator.rb', line 40

def validate_file!(filename)
  result = validate_file(filename)
  raise ValidatorError, "Invalid SBOM: #{result.errors.join(', ')}" if result.invalid?

  result
end

#validate_string(content, sbom_type: nil) ⇒ Object



47
48
49
50
# File 'lib/sbom/validator.rb', line 47

def validate_string(content, sbom_type: nil)
  sbom_type ||= detect_type_from_content(content)
  validate_content(content, sbom_type)
end

#validate_string!(content, sbom_type: nil) ⇒ Object

Raises:



52
53
54
55
56
57
# File 'lib/sbom/validator.rb', line 52

def validate_string!(content, sbom_type: nil)
  result = validate_string(content, sbom_type: sbom_type)
  raise ValidatorError, "Invalid SBOM: #{result.errors.join(', ')}" if result.invalid?

  result
end