Validate shared domain documents offline
This commit is contained in:
@@ -175,6 +175,8 @@ or credentials.
|
|||||||
|
|
||||||
## Stage 3 — Validate Resolved Shared Documents
|
## Stage 3 — Validate Resolved Shared Documents
|
||||||
|
|
||||||
|
**Status:** Complete.
|
||||||
|
|
||||||
**Goal:** Separate shared conformance from consumer requirements.
|
**Goal:** Separate shared conformance from consumer requirements.
|
||||||
|
|
||||||
**Inspect:** Bundled schemas/semantics and `assets.FS()`.
|
**Inspect:** Bundled schemas/semantics and `assets.FS()`.
|
||||||
|
|||||||
123
internal/integrations/domainrefs/validate.go
Normal file
123
internal/integrations/domainrefs/validate.go
Normal file
@@ -0,0 +1,123 @@
|
|||||||
|
package domainrefs
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"fmt"
|
||||||
|
"io/fs"
|
||||||
|
"sort"
|
||||||
|
"sync"
|
||||||
|
|
||||||
|
"gitea.maximumdirect.net/eric/notarius/assets"
|
||||||
|
"github.com/santhosh-tekuri/jsonschema/v6"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
glossaryID = "https://schemas.maximumdirect.net/glossary/v0.2.0/schema.json"
|
||||||
|
partyID = "https://schemas.maximumdirect.net/rpg/party/v0.2.0/schema.json"
|
||||||
|
)
|
||||||
|
|
||||||
|
var compiled struct {
|
||||||
|
sync.Once
|
||||||
|
glossary, party *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 compileSchemas() {
|
||||||
|
compiler := jsonschema.NewCompiler()
|
||||||
|
compiler.UseLoader(offlineLoader{})
|
||||||
|
for _, resource := range []struct{ id, path string }{
|
||||||
|
{glossaryID, "shared-domain/glossary/v0.2.0/schema.json"},
|
||||||
|
{partyID, "shared-domain/rpg/party/v0.2.0/schema.json"},
|
||||||
|
} {
|
||||||
|
content, err := fs.ReadFile(assets.FS(), resource.path)
|
||||||
|
if err != nil {
|
||||||
|
compiled.err = err
|
||||||
|
return
|
||||||
|
}
|
||||||
|
doc, err := jsonschema.UnmarshalJSON(bytes.NewReader(content))
|
||||||
|
if err == nil {
|
||||||
|
err = compiler.AddResource(resource.id, doc)
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
compiled.err = fmt.Errorf("register schema %s: %w", resource.id, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
compiled.glossary, compiled.err = compiler.Compile(glossaryID)
|
||||||
|
if compiled.err == nil {
|
||||||
|
compiled.party, compiled.err = compiler.Compile(partyID)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ValidateGlossary validates the entire document and per-collection entry IDs.
|
||||||
|
func ValidateGlossary(doc map[string]any) error {
|
||||||
|
compiled.Do(compileSchemas)
|
||||||
|
if compiled.err != nil {
|
||||||
|
return fmt.Errorf("prepare shared schemas: %w", compiled.err)
|
||||||
|
}
|
||||||
|
if err := compiled.glossary.Validate(doc); err != nil {
|
||||||
|
return fmt.Errorf("invalid shared glossary: %w", err)
|
||||||
|
}
|
||||||
|
collections := doc["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)
|
||||||
|
ids := make(map[string]bool, len(entries))
|
||||||
|
for _, raw := range entries {
|
||||||
|
id := raw.(map[string]any)["id"].(string)
|
||||||
|
if ids[id] {
|
||||||
|
return fmt.Errorf("glossary collection %q has duplicate entry ID %q", name, id)
|
||||||
|
}
|
||||||
|
ids[id] = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// ValidateParty validates the complete resolved party document.
|
||||||
|
func ValidateParty(doc map[string]any) error {
|
||||||
|
compiled.Do(compileSchemas)
|
||||||
|
if compiled.err != nil {
|
||||||
|
return fmt.Errorf("prepare shared schemas: %w", compiled.err)
|
||||||
|
}
|
||||||
|
if err := compiled.party.Validate(doc); err != nil {
|
||||||
|
return fmt.Errorf("invalid shared party: %w", err)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// SelectCollections returns sorted, unique collection names. A nil selection
|
||||||
|
// uses the caller's defaults; an explicit empty selection remains empty. The
|
||||||
|
// caller chooses defaults appropriate to its task and validates the document first.
|
||||||
|
func SelectCollections(doc map[string]any, selection, defaults []string) ([]string, error) {
|
||||||
|
if selection == nil {
|
||||||
|
selection = defaults
|
||||||
|
}
|
||||||
|
collections := doc["collections"].(map[string]any)
|
||||||
|
seen := make(map[string]bool, len(selection))
|
||||||
|
result := make([]string, 0, len(selection))
|
||||||
|
for _, name := range selection {
|
||||||
|
if name == "" {
|
||||||
|
return nil, fmt.Errorf("collection name must not be empty")
|
||||||
|
}
|
||||||
|
if _, ok := collections[name]; !ok {
|
||||||
|
return nil, fmt.Errorf("selected collection %q does not exist", name)
|
||||||
|
}
|
||||||
|
if !seen[name] {
|
||||||
|
seen[name] = true
|
||||||
|
result = append(result, name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
sort.Strings(result)
|
||||||
|
return result, nil
|
||||||
|
}
|
||||||
107
internal/integrations/domainrefs/validate_test.go
Normal file
107
internal/integrations/domainrefs/validate_test.go
Normal file
@@ -0,0 +1,107 @@
|
|||||||
|
package domainrefs
|
||||||
|
|
||||||
|
import (
|
||||||
|
"reflect"
|
||||||
|
"strings"
|
||||||
|
"sync"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func document(t *testing.T, source string) map[string]any {
|
||||||
|
t.Helper()
|
||||||
|
doc, err := Load("fixture.json", "application/json", []byte(source))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
return doc
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGlossaryConformance(t *testing.T) {
|
||||||
|
for _, test := range []struct {
|
||||||
|
name, source string
|
||||||
|
valid bool
|
||||||
|
}{
|
||||||
|
{"empty", `{"schema_version":"maximumdirect.glossary.v1","collections":{}}`, true},
|
||||||
|
{"extensions and aliases", `{"schema_version":"maximumdirect.glossary.v1","extension":{"count":42},"collections":{"a":{"extension":true,"provenance":{"extension":1.5},"entries":[{"id":"x","name":"N","category":"c","extension":{"instruction":"data"}},{"id":"y","name":"N","category":"c","aliases":null},{"id":"z","name":"N","category":"c","aliases":[]}]},"b":{"entries":[{"id":"x","name":"N","category":"c","aliases":["Alias"]}]}}}`, true},
|
||||||
|
{"empty entries", `{"schema_version":"maximumdirect.glossary.v1","collections":{"a":{"entries":[]}}}`, true},
|
||||||
|
{"duplicate IDs", `{"schema_version":"maximumdirect.glossary.v1","collections":{"a":{"entries":[{"id":"x","name":"N","category":"c"},{"id":"x","name":"N","category":"c"}]}}}`, false},
|
||||||
|
{"invalid unselected collection", `{"schema_version":"maximumdirect.glossary.v1","collections":{"a":{"entries":[]},"b":{"entries":[{"id":"x","name":"N","category":"c","aliases":5}]}}}`, false},
|
||||||
|
{"invalid provenance", `{"schema_version":"maximumdirect.glossary.v1","collections":{"a":{"entries":[],"provenance":null}}}`, false},
|
||||||
|
{"wrong marker", `{"schema_version":"wrong","collections":{}}`, false},
|
||||||
|
} {
|
||||||
|
t.Run(test.name, func(t *testing.T) {
|
||||||
|
doc := document(t, test.source)
|
||||||
|
err := ValidateGlossary(doc)
|
||||||
|
if (err == nil) != test.valid {
|
||||||
|
t.Fatalf("ValidateGlossary = %v, valid %v", err, test.valid)
|
||||||
|
}
|
||||||
|
if test.name == "extensions and aliases" && doc["extension"].(map[string]any)["count"] != int64(42) {
|
||||||
|
t.Fatal("validation changed resolved extension")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPartyConformance(t *testing.T) {
|
||||||
|
for _, test := range []struct {
|
||||||
|
name, source string
|
||||||
|
valid bool
|
||||||
|
}{
|
||||||
|
{"empty", `{"schema_version":"maximumdirect.rpg.party.v1","characters":{}}`, true},
|
||||||
|
{"optional and extensions", `{"schema_version":"maximumdirect.rpg.party.v1","extension":true,"characters":{"a":{"extension":1,"character":{"name":"Same","extension":{"v":1.5}}},"b":{"character":{"name":"Same","aliases":null,"classes":[]},"player":{"name":"P","extension":true}},"c":{"character":{"name":"C","aliases":[],"classes":[{"name":"Mage","level":9223372036854775807,"extension":1}]}}}}`, true},
|
||||||
|
{"null classes", `{"schema_version":"maximumdirect.rpg.party.v1","characters":{"a":{"character":{"name":"N","classes":null}}}}`, false},
|
||||||
|
{"invalid player", `{"schema_version":"maximumdirect.rpg.party.v1","characters":{"a":{"character":{"name":"N"},"player":null}}}`, false},
|
||||||
|
} {
|
||||||
|
t.Run(test.name, func(t *testing.T) {
|
||||||
|
err := ValidateParty(document(t, test.source))
|
||||||
|
if (err == nil) != test.valid {
|
||||||
|
t.Fatalf("ValidateParty = %v, valid %v", err, test.valid)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCollectionSelection(t *testing.T) {
|
||||||
|
doc := document(t, `{"schema_version":"maximumdirect.glossary.v1","collections":{"a":{"entries":[]},"B":{"entries":[]}}}`)
|
||||||
|
if err := ValidateGlossary(doc); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
for _, test := range []struct {
|
||||||
|
name string
|
||||||
|
selection, defaults []string
|
||||||
|
want []string
|
||||||
|
failure string
|
||||||
|
}{
|
||||||
|
{"default", nil, []string{"a"}, []string{"a"}, ""},
|
||||||
|
{"empty", []string{}, []string{"a"}, []string{}, ""},
|
||||||
|
{"deduplicated and sorted", []string{"a", "B", "a"}, nil, []string{"B", "a"}, ""},
|
||||||
|
{"exact", []string{"b"}, nil, nil, "b"},
|
||||||
|
{"empty name", []string{""}, nil, nil, "empty"},
|
||||||
|
} {
|
||||||
|
t.Run(test.name, func(t *testing.T) {
|
||||||
|
got, err := SelectCollections(doc, test.selection, test.defaults)
|
||||||
|
if test.failure != "" {
|
||||||
|
if err == nil || !strings.Contains(err.Error(), test.failure) {
|
||||||
|
t.Fatalf("selection error = %v", err)
|
||||||
|
}
|
||||||
|
} else if err != nil || !reflect.DeepEqual(got, test.want) {
|
||||||
|
t.Fatalf("selection = %#v, %v; want %#v", got, err, test.want)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestConcurrentValidation(t *testing.T) {
|
||||||
|
doc := document(t, `{"schema_version":"maximumdirect.rpg.party.v1","characters":{}}`)
|
||||||
|
var wg sync.WaitGroup
|
||||||
|
for range 8 {
|
||||||
|
wg.Add(1)
|
||||||
|
go func() {
|
||||||
|
defer wg.Done()
|
||||||
|
if err := ValidateParty(doc); err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
wg.Wait()
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user