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 }