aboutsummaryrefslogtreecommitdiff
path: root/spdxlib/documents.go
diff options
context:
space:
mode:
Diffstat (limited to 'spdxlib/documents.go')
-rw-r--r--spdxlib/documents.go31
1 files changed, 31 insertions, 0 deletions
diff --git a/spdxlib/documents.go b/spdxlib/documents.go
index 8560f08..dc41803 100644
--- a/spdxlib/documents.go
+++ b/spdxlib/documents.go
@@ -7,6 +7,7 @@ import (
"github.com/spdx/tools-golang/spdx/common"
"github.com/spdx/tools-golang/spdx/v2_1"
"github.com/spdx/tools-golang/spdx/v2_2"
+ "github.com/spdx/tools-golang/spdx/v2_3"
)
// ValidateDocument2_1 returns an error if the Document is found to be invalid, or nil if the Document is valid.
@@ -68,3 +69,33 @@ func ValidateDocument2_2(doc *v2_2.Document) error {
return nil
}
+
+// ValidateDocument2_3 returns an error if the Document is found to be invalid, or nil if the Document is valid.
+// Currently, this only verifies that all Element IDs mentioned in Relationships exist in the Document as either a
+// Package or an UnpackagedFile.
+func ValidateDocument2_3(doc *v2_3.Document) error {
+ // cache a map of package IDs for quick lookups
+ validElementIDs := make(map[common.ElementID]bool)
+ for _, docPackage := range doc.Packages {
+ validElementIDs[docPackage.PackageSPDXIdentifier] = true
+ }
+
+ for _, unpackagedFile := range doc.Files {
+ validElementIDs[unpackagedFile.FileSPDXIdentifier] = true
+ }
+
+ // add the Document element ID
+ validElementIDs[common.MakeDocElementID("", "DOCUMENT").ElementRefID] = true
+
+ for _, relationship := range doc.Relationships {
+ if !validElementIDs[relationship.RefA.ElementRefID] {
+ return fmt.Errorf("%s used in relationship but no such package exists", string(relationship.RefA.ElementRefID))
+ }
+
+ if !validElementIDs[relationship.RefB.ElementRefID] {
+ return fmt.Errorf("%s used in relationship but no such package exists", string(relationship.RefB.ElementRefID))
+ }
+ }
+
+ return nil
+}