• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
2package spdxlib
3
4import (
5	"fmt"
6
7	"github.com/spdx/tools-golang/spdx/common"
8	"github.com/spdx/tools-golang/spdx/v2_1"
9	"github.com/spdx/tools-golang/spdx/v2_2"
10	"github.com/spdx/tools-golang/spdx/v2_3"
11)
12
13// ValidateDocument2_1 returns an error if the Document is found to be invalid, or nil if the Document is valid.
14// Currently, this only verifies that all Element IDs mentioned in Relationships exist in the Document as either a
15// Package or an UnpackagedFile.
16func ValidateDocument2_1(doc *v2_1.Document) error {
17	// cache a map of valid package IDs for quick lookups
18	validElementIDs := make(map[common.ElementID]bool)
19	for _, docPackage := range doc.Packages {
20		validElementIDs[docPackage.PackageSPDXIdentifier] = true
21	}
22
23	for _, unpackagedFile := range doc.Files {
24		validElementIDs[unpackagedFile.FileSPDXIdentifier] = true
25	}
26
27	// add the Document element ID
28	validElementIDs[common.MakeDocElementID("", "DOCUMENT").ElementRefID] = true
29
30	for _, relationship := range doc.Relationships {
31		if !validElementIDs[relationship.RefA.ElementRefID] {
32			return fmt.Errorf("%s used in relationship but no such package exists", string(relationship.RefA.ElementRefID))
33		}
34
35		if !validElementIDs[relationship.RefB.ElementRefID] {
36			return fmt.Errorf("%s used in relationship but no such package exists", string(relationship.RefB.ElementRefID))
37		}
38	}
39
40	return nil
41}
42
43// ValidateDocument2_2 returns an error if the Document is found to be invalid, or nil if the Document is valid.
44// Currently, this only verifies that all Element IDs mentioned in Relationships exist in the Document as either a
45// Package or an UnpackagedFile.
46func ValidateDocument2_2(doc *v2_2.Document) error {
47	// cache a map of package IDs for quick lookups
48	validElementIDs := make(map[common.ElementID]bool)
49	for _, docPackage := range doc.Packages {
50		validElementIDs[docPackage.PackageSPDXIdentifier] = true
51	}
52
53	for _, unpackagedFile := range doc.Files {
54		validElementIDs[unpackagedFile.FileSPDXIdentifier] = true
55	}
56
57	// add the Document element ID
58	validElementIDs[common.MakeDocElementID("", "DOCUMENT").ElementRefID] = true
59
60	for _, relationship := range doc.Relationships {
61		if !validElementIDs[relationship.RefA.ElementRefID] {
62			return fmt.Errorf("%s used in relationship but no such package exists", string(relationship.RefA.ElementRefID))
63		}
64
65		if !validElementIDs[relationship.RefB.ElementRefID] {
66			return fmt.Errorf("%s used in relationship but no such package exists", string(relationship.RefB.ElementRefID))
67		}
68	}
69
70	return nil
71}
72
73// ValidateDocument2_3 returns an error if the Document is found to be invalid, or nil if the Document is valid.
74// Currently, this only verifies that all Element IDs mentioned in Relationships exist in the Document as either a
75// Package or an UnpackagedFile.
76func ValidateDocument2_3(doc *v2_3.Document) error {
77	// cache a map of package IDs for quick lookups
78	validElementIDs := make(map[common.ElementID]bool)
79	for _, docPackage := range doc.Packages {
80		validElementIDs[docPackage.PackageSPDXIdentifier] = true
81	}
82
83	for _, unpackagedFile := range doc.Files {
84		validElementIDs[unpackagedFile.FileSPDXIdentifier] = true
85	}
86
87	// add the Document element ID
88	validElementIDs[common.MakeDocElementID("", "DOCUMENT").ElementRefID] = true
89
90	for _, relationship := range doc.Relationships {
91		if !validElementIDs[relationship.RefA.ElementRefID] {
92			return fmt.Errorf("%s used in relationship but no such package exists", string(relationship.RefA.ElementRefID))
93		}
94
95		if !validElementIDs[relationship.RefB.ElementRefID] {
96			return fmt.Errorf("%s used in relationship but no such package exists", string(relationship.RefB.ElementRefID))
97		}
98	}
99
100	return nil
101}
102