113 lines
3.0 KiB
Go
113 lines
3.0 KiB
Go
|
|
// Package domaininput validates completed shared domain mappings.
|
||
|
|
package domaininput
|
||
|
|
|
||
|
|
import (
|
||
|
|
"bytes"
|
||
|
|
"embed"
|
||
|
|
"encoding/json"
|
||
|
|
"fmt"
|
||
|
|
"sort"
|
||
|
|
"sync"
|
||
|
|
|
||
|
|
"github.com/santhosh-tekuri/jsonschema/v6"
|
||
|
|
)
|
||
|
|
|
||
|
|
// SourceRevision identifies the release from which the bundled resources came.
|
||
|
|
const SourceRevision = "f7e3cedeca11ed903311cdf29ca4d81fb1f216b7"
|
||
|
|
|
||
|
|
const schemaBase = "https://schemas.maximumdirect.net/"
|
||
|
|
|
||
|
|
//go:embed resources/*/v0.2.0/schema.json resources/rpg/*/v0.2.0/schema.json
|
||
|
|
var resources embed.FS
|
||
|
|
|
||
|
|
var compiled struct {
|
||
|
|
sync.Once
|
||
|
|
schemas map[string]*jsonschema.Schema
|
||
|
|
err error
|
||
|
|
}
|
||
|
|
|
||
|
|
type offlineLoader struct{}
|
||
|
|
|
||
|
|
func (offlineLoader) Load(url string) (any, error) {
|
||
|
|
return nil, fmt.Errorf("schema resource %q is not bundled", url)
|
||
|
|
}
|
||
|
|
|
||
|
|
func schema(name string) (*jsonschema.Schema, error) {
|
||
|
|
compiled.Do(func() {
|
||
|
|
compiler := jsonschema.NewCompiler()
|
||
|
|
compiler.UseLoader(offlineLoader{})
|
||
|
|
compiled.schemas = make(map[string]*jsonschema.Schema)
|
||
|
|
names := []string{"glossary", "rpg/party", "rpg/campaign"}
|
||
|
|
for _, name := range names {
|
||
|
|
path := name + "/v0.2.0/schema.json"
|
||
|
|
data, err := resources.ReadFile("resources/" + path)
|
||
|
|
if err != nil {
|
||
|
|
compiled.err = err
|
||
|
|
return
|
||
|
|
}
|
||
|
|
decoder := json.NewDecoder(bytes.NewReader(data))
|
||
|
|
decoder.UseNumber()
|
||
|
|
var document any
|
||
|
|
if err := decoder.Decode(&document); err != nil {
|
||
|
|
compiled.err = err
|
||
|
|
return
|
||
|
|
}
|
||
|
|
if err := compiler.AddResource(schemaBase+path, document); err != nil {
|
||
|
|
compiled.err = err
|
||
|
|
return
|
||
|
|
}
|
||
|
|
}
|
||
|
|
for _, name := range names {
|
||
|
|
var err error
|
||
|
|
compiled.schemas[name], err = compiler.Compile(schemaBase + name + "/v0.2.0/schema.json")
|
||
|
|
if err != nil {
|
||
|
|
compiled.err = err
|
||
|
|
return
|
||
|
|
}
|
||
|
|
}
|
||
|
|
})
|
||
|
|
return compiled.schemas[name], compiled.err
|
||
|
|
}
|
||
|
|
|
||
|
|
func validate(name string, value map[string]any) error {
|
||
|
|
s, err := schema(name)
|
||
|
|
if err != nil {
|
||
|
|
return fmt.Errorf("compile shared schemas: %w", err)
|
||
|
|
}
|
||
|
|
if err := s.Validate(value); err != nil {
|
||
|
|
return fmt.Errorf("%s: %w", name, err)
|
||
|
|
}
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
|
||
|
|
// ValidateCampaign validates a completed shared campaign mapping.
|
||
|
|
func ValidateCampaign(value map[string]any) error { return validate("rpg/campaign", value) }
|
||
|
|
|
||
|
|
// ValidateParty validates a completed shared party mapping.
|
||
|
|
func ValidateParty(value map[string]any) error { return validate("rpg/party", value) }
|
||
|
|
|
||
|
|
// ValidateGlossary validates a completed shared glossary and collection IDs.
|
||
|
|
func ValidateGlossary(value map[string]any) error {
|
||
|
|
if err := validate("glossary", value); err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
collections := value["collections"].(map[string]any)
|
||
|
|
names := make([]string, 0, len(collections))
|
||
|
|
for name := range collections {
|
||
|
|
names = append(names, name)
|
||
|
|
}
|
||
|
|
sort.Strings(names)
|
||
|
|
for _, name := range names {
|
||
|
|
entries := collections[name].(map[string]any)["entries"].([]any)
|
||
|
|
seen := make(map[string]bool, len(entries))
|
||
|
|
for _, entry := range entries {
|
||
|
|
id := entry.(map[string]any)["id"].(string)
|
||
|
|
if seen[id] {
|
||
|
|
return fmt.Errorf("collection %q: duplicate entry id %q", name, id)
|
||
|
|
}
|
||
|
|
seen[id] = true
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return nil
|
||
|
|
}
|