1
0
Fork 0
mirror of https://github.com/gocsaf/csaf.git synced 2025-12-22 11:55:40 +01:00

Validate CSAF documents against JSON schema.

This commit is contained in:
Sascha L. Teichmann 2021-12-02 23:38:09 +01:00
parent b21cef4677
commit 78f0b2db0b
6 changed files with 1417 additions and 0 deletions

34
csaf/validation.go Normal file
View file

@ -0,0 +1,34 @@
package csaf
import (
_ "embed"
"github.com/xeipuuv/gojsonschema"
)
//go:embed schema/csaf_json_schema.json
var schema string
// Validate validates the document data against the JSON schema
// of CSAF.
func Validate(data []byte) ([]string, error) {
schemaLoader := gojsonschema.NewStringLoader(schema)
documentLoader := gojsonschema.NewStringLoader(string(data))
result, err := gojsonschema.Validate(schemaLoader, documentLoader)
if err != nil {
return nil, err
}
if result.Valid() {
return nil, nil
}
errors := result.Errors()
res := make([]string, len(errors))
for i, e := range errors {
res[i] = e.String()
}
return res, nil
}