Fix broken schema and add tests

This commit is contained in:
Jan Eitzinger
2022-09-20 10:27:23 +02:00
parent 207254a9e2
commit 2fdf1c14be
9 changed files with 200 additions and 126 deletions

View File

@@ -9,7 +9,8 @@ import (
"encoding/json"
"fmt"
"io"
"net/url"
"path/filepath"
"strings"
"github.com/ClusterCockpit/cc-backend/pkg/log"
"github.com/santhosh-tekuri/jsonschema/v5"
@@ -27,31 +28,22 @@ const (
//go:embed schemas/*
var schemaFiles embed.FS
func Load(s string) (io.ReadCloser, error) {
u, err := url.Parse(s)
if err != nil {
return nil, err
}
f := u.Path
return schemaFiles.Open(f)
}
func init() {
jsonschema.Loaders["embedFS"] = Load
}
func Validate(k Kind, r io.Reader) (err error) {
jsonschema.Loaders["embedfs"] = func(s string) (io.ReadCloser, error) {
f := filepath.Join("schemas", strings.Split(s, "//")[1])
return schemaFiles.Open(f)
}
var s *jsonschema.Schema
switch k {
case Meta:
s, err = jsonschema.Compile("embedFS://schemas/job-meta.schema.json")
s, err = jsonschema.Compile("embedfs://job-meta.schema.json")
case Data:
s, err = jsonschema.Compile("embedFS://schemas/job-data.schema.json")
s, err = jsonschema.Compile("embedfs://job-data.schema.json")
case ClusterCfg:
s, err = jsonschema.Compile("embedFS://schemas/cluster.schema.json")
s, err = jsonschema.Compile("embedfs://cluster.schema.json")
case Config:
s, err = jsonschema.Compile("embedFS://schemas/config.schema.json")
s, err = jsonschema.Compile("embedfs://config.schema.json")
default:
return fmt.Errorf("unkown schema kind ")
}