mirror of
https://github.com/gocsaf/csaf.git
synced 2025-12-22 05:40:11 +01:00
Hook into the JSON schema loader to load needed schemas from embedding.
This commit is contained in:
parent
8c272fef2a
commit
a96597206e
6 changed files with 435 additions and 3 deletions
|
|
@ -4,8 +4,11 @@ import (
|
|||
"context"
|
||||
_ "embed"
|
||||
"encoding/json"
|
||||
"log"
|
||||
"net/url"
|
||||
"sort"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/qri-io/jsonschema"
|
||||
)
|
||||
|
|
@ -13,10 +16,49 @@ import (
|
|||
//go:embed schema/csaf_json_schema.json
|
||||
var schema []byte
|
||||
|
||||
//go:embed schema/cvss-v2.0.json
|
||||
var cvss20 []byte
|
||||
|
||||
//go:embed schema/cvss-v3.0.json
|
||||
var cvss30 []byte
|
||||
|
||||
//go:embed schema/cvss-v3.1.json
|
||||
var cvss31 []byte
|
||||
|
||||
func embedLoader(ctx context.Context, uri *url.URL, schema *jsonschema.Schema) error {
|
||||
|
||||
var data []byte
|
||||
switch u := uri.String(); u {
|
||||
case "https://www.first.org/cvss/cvss-v2.0.json":
|
||||
data = cvss20
|
||||
case "https://www.first.org/cvss/cvss-v3.0.json":
|
||||
data = cvss30
|
||||
case "https://www.first.org/cvss/cvss-v3.1.json":
|
||||
data = cvss31
|
||||
default:
|
||||
log.Printf("escaped schema loader: %s\n", u)
|
||||
return jsonschema.HTTPSchemaLoader(ctx, uri, schema)
|
||||
}
|
||||
if schema == nil {
|
||||
schema = &jsonschema.Schema{}
|
||||
}
|
||||
return json.Unmarshal(data, schema)
|
||||
}
|
||||
|
||||
var registerEmbedLoaderOnce sync.Once
|
||||
|
||||
func registerEmbedLoader() {
|
||||
// Hook into schema loading.
|
||||
slr := jsonschema.GetSchemaLoaderRegistry()
|
||||
slr.Register("https", embedLoader)
|
||||
}
|
||||
|
||||
// ValidateCSAF validates the document data against the JSON schema
|
||||
// of CSAF.
|
||||
func ValidateCSAF(doc interface{}) ([]string, error) {
|
||||
|
||||
registerEmbedLoaderOnce.Do(registerEmbedLoader)
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
rs := &jsonschema.Schema{}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue