Bundle and validate the shared glossary contract

This commit is contained in:
2026-11-29 18:09:08 +00:00
parent c0d2280305
commit 3e287503eb
13 changed files with 474 additions and 1 deletions

View File

@@ -0,0 +1,3 @@
# Upstream compatibility guidance
See the [compatibility guide from the selected schemas distribution](https://forge.maximumdirect.net/eric/schemas/src/commit/f7e3cedeca11ed903311cdf29ca4d81fb1f216b7/docs/compatibility.md).

View File

@@ -0,0 +1,3 @@
# Upstream validation guidance
See the [validation guide from the selected schemas distribution](https://forge.maximumdirect.net/eric/schemas/src/commit/f7e3cedeca11ed903311cdf29ca4d81fb1f216b7/docs/validation.md).

View File

@@ -0,0 +1,21 @@
schema_version: maximumdirect.glossary.v1
collections:
campaign:
entries:
- id: lighthouse
name: Glass Lantern
category: place
aliases: [The Lantern]
description: A fictional lighthouse at the entrance to the bay.
spells:
provenance:
id: lantern-coast-original
title: Synthetic campaign spells
version: "1"
license: CC0-1.0
entries:
- id: ember-veil
name: Ember Veil
category: spell
aliases: [Veil of Embers]
casting_time: one action

View File

@@ -0,0 +1,16 @@
schema_version: maximumdirect.glossary.v1
collections:
clubs:
sport: association football
entries:
- id: harbour
name: Harbour Wanderers
category: club
aliases: []
plural: null
home_ground: Lantern Park
venues:
entries:
- id: harbour
name: Lantern Park
category: venue

View File

@@ -0,0 +1,12 @@
{
"schema_version": "maximumdirect.glossary.v1",
"collections": {
"measurements": {
"unit_system": "metric",
"entries": [
{"id": "temperature", "name": "Air temperature", "category": "measurement", "aliases": ["Temperature"], "unit": "degree Celsius"},
{"id": "precipitation", "name": "Precipitation", "category": "measurement", "aliases": null, "unit": "millimetre"}
]
}
}
}

View File

@@ -0,0 +1,15 @@
schema_version: maximumdirect.glossary.v1
collections:
measurements:
unit_system: metric
entries:
- id: temperature
name: Air temperature
category: measurement
aliases: [Temperature]
unit: degree Celsius
- id: precipitation
name: Precipitation
category: measurement
aliases: null
unit: millimetre

View File

@@ -0,0 +1,51 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://schemas.maximumdirect.net/glossary/v0.2.0/schema.json",
"title": "MAXIMUMDIRECT Glossary v1",
"type": "object",
"required": ["schema_version", "collections"],
"properties": {
"schema_version": {"const": "maximumdirect.glossary.v1"},
"collections": {
"type": "object",
"propertyNames": {"minLength": 1},
"additionalProperties": {"$ref": "#/$defs/collection"}
}
},
"$defs": {
"text": {"type": "string", "minLength": 1},
"aliases": {"type": ["array", "null"], "items": {"$ref": "#/$defs/text"}},
"entry": {
"type": "object",
"required": ["id", "name", "category"],
"properties": {
"id": {"$ref": "#/$defs/text"},
"name": {"$ref": "#/$defs/text"},
"category": {"$ref": "#/$defs/text"},
"description": {"type": "string"},
"aliases": {"$ref": "#/$defs/aliases"},
"plural": {"type": ["string", "null"], "minLength": 1}
}
},
"provenance": {
"type": "object",
"properties": {
"id": {"$ref": "#/$defs/text"},
"title": {"$ref": "#/$defs/text"},
"version": {"type": "string"},
"url": {"type": "string"},
"license": {"type": "string"}
}
},
"partialCollection": {
"type": "object",
"properties": {
"entries": {"type": "array", "items": {"$ref": "#/$defs/entry"}},
"provenance": {"$ref": "#/$defs/provenance"}
}
},
"collection": {
"allOf": [{"$ref": "#/$defs/partialCollection"}, {"required": ["entries"]}]
}
}
}

View File

@@ -0,0 +1,83 @@
# Glossary contract
## Native loading boundary
Full conformance includes acquisition through the public mapping-root handoff of
[ConfigKit v1.4.0](https://forge.maximumdirect.net/base-layer/configkit/src/commit/4a7ae664a6287d71cba20f59c7c5b37b6f05b0a3/docs/config.md),
followed by this schema and its semantic checks. ConfigKit owns syntax, structural
controls, numeric binding, and resource limits, including its 8 MiB source limit.
Dynamic integers bind as int64 and fractional/exponent values as finite float64;
authored decimal precision is not guaranteed. Preserve supported resolved values.
`extends` is structural input and source-root `config` is a control namespace;
nested ordinary `config` follows upstream rules. Other supported domain extensions
remain permitted. Schema validation adds no trimming, case folding, or defaults.
Structural validation alone does not prove native loading compatibility. The
pinned upstream contract defines this version's boundary independently of later
changes to the repository's general guidance.
Contract: `maximumdirect.glossary.v1`. [Schema](schema.json).
The [validation profile](../../docs/validation.md) and [compatibility rules](../../docs/compatibility.md)
apply. Start with [campaign vocabulary](examples/glossary.yml),
[weather](examples/weather.yml), or [sports](examples/sports.yml).
| Field | Required | Meaning |
| --- | --- | --- |
| `schema_version` | Yes | Exactly `maximumdirect.glossary.v1`. |
| `collections` | Yes | Mapping of nonempty collection names to collections; may be empty. |
| `collections.<name>.entries` | Yes | List of entries; may be empty. |
| `entries[].id` | Yes | Stable, nonempty identity unique within this collection. |
| `entries[].name` | Yes | Nonempty canonical display name. |
| `entries[].category` | Yes | Nonempty domain-defined category. |
| `entries[].description` | No | Description; an empty string is valid. |
| `entries[].aliases` | No | Alternative names for the same entity. May be null, empty, or a list of nonempty strings. |
| `entries[].plural` | No | Nonempty plural display form, or null. |
| `collections.<name>.provenance` | No | Source metadata object, possibly empty. Optional `id` and `title` are nonempty strings; optional `version`, `url`, and `license` are strings that may be empty. |
All objects permit extensions. Optional fields accept omission; only `aliases`
and `plural` additionally accept null. Supported resolved values are preserved; schema validation adds no trimming,
case folding, coercion, or defaults. IDs and names compare exactly. Distinct
collections can reuse an ID. Display names, aliases, and categories need not be
unique. Lists retain their authored order but establish no precedence or execution
order. Collection names and categories are strings chosen by the domain, not a
fixed set of D&D labels.
For example, `spells` entries contain names, aliases, and optionally extended
domain information such as `casting_time`. Their collection's provenance can
identify a source catalog. Domain-specific metadata can use extension fields.
Provenance describes origin and applicability;
it is not transcript evidence or a spell rule. If different source contexts
matter, use separately named collections with their own provenance. A consumer
can derive its recognition input from these entries instead of requiring a second
operator-maintained spell list. Embedded vocabulary, source precedence,
normalization, inference, and conflicts remain consumer policy.
Entry-ID uniqueness is a semantic rule checked separately from JSON Schema.
The [validator](../../docs/validation.md) performs both checks. No cross-document identity or
reference-resolution check is part of this initial contract.
## Extensions and task instructions
Extensions describe domain information. Consumers forwarding a shared document
should retain them; task-specific projections may intentionally omit them.
Acceptance and preservation do not claim interpretation. Defined fields retain
their schema constraints in an extended document.
Task instructions belong to consumer configuration. `instruction` is not a
defined field in this contract. Like other unknown fields, it can be retained
without acquiring instruction semantics. Maintained examples contain domain
data only.
## Authoring contributions
`#/$defs/partialCollection` is an editor-assistance definition with optional
`entries`. `#/$defs/entry` and `#/$defs/collection` assist entry and complete
collection editing. A partial contribution is not a standalone valid domain
document. ConfigKit owns imports, inheritance, and composition before validation
of the complete envelope.
## Differences from the reference catalog and Audita v4.2
The old format maps collection names directly at the root. The shared format
requires `schema_version` and `collections`. It removes defined collection
instructions, permits structural extensions, and adds defined provenance.
Existing consumers must explicitly adopt this envelope and its semantics.

View File

@@ -0,0 +1,52 @@
// Package sharedglossary implements the boundary for shared glossary documents.
package sharedglossary
import (
_ "embed"
"encoding/json"
"fmt"
"github.com/santhosh-tekuri/jsonschema/v6"
)
const schemaID = "https://schemas.maximumdirect.net/glossary/v0.2.0/schema.json"
//go:embed assets/glossary/v0.2.0/schema.json
var schemaData []byte
// Validator validates completed shared glossary envelopes.
type Validator struct {
schema *jsonschema.Schema
}
// NewValidator compiles the locally bundled schema for reuse.
func NewValidator() (*Validator, error) {
var document any
if err := json.Unmarshal(schemaData, &document); err != nil {
return nil, fmt.Errorf("decode shared glossary schema: %w", err)
}
compiler := jsonschema.NewCompiler()
compiler.UseLoader(offlineLoader{})
if err := compiler.AddResource(schemaID, document); err != nil {
return nil, fmt.Errorf("register shared glossary schema: %w", err)
}
schema, err := compiler.Compile(schemaID)
if err != nil {
return nil, fmt.Errorf("compile shared glossary schema: %w", err)
}
return &Validator{schema: schema}, nil
}
// Validate checks an envelope without modifying it, retaining source context.
func (v *Validator) Validate(document map[string]any, source string) error {
if err := v.schema.Validate(document); err != nil {
return fmt.Errorf("shared glossary %s: %w", source, err)
}
return nil
}
type offlineLoader struct{}
func (offlineLoader) Load(url string) (any, error) {
return nil, fmt.Errorf("unregistered schema resource %q", url)
}

View File

@@ -0,0 +1,81 @@
package sharedglossary
import (
"encoding/json"
"os"
"path/filepath"
"reflect"
"strings"
"testing"
"go.yaml.in/yaml/v4"
)
func TestValidate(t *testing.T) {
validator, err := NewValidator()
if err != nil {
t.Fatal(err)
}
for _, tc := range []struct {
name, document, location string
valid bool
}{
{"ordinary", `{"schema_version":"maximumdirect.glossary.v1","collections":{"terms":{"entries":[{"id":"one","name":"One","category":"number"}]}}}`, "", true},
{"empty collections", `{"schema_version":"maximumdirect.glossary.v1","collections":{}}`, "", true},
{"nullable and extensions", `{"schema_version":"maximumdirect.glossary.v1","extra":true,"collections":{"terms":{"instruction":42,"provenance":{"extra":true},"entries":[{"id":"one","name":"One","category":"number","aliases":null,"plural":null,"extra":{"n":1}}]}}}`, "", true},
{"missing marker", `{"collections":{}}`, "schema_version", false},
{"wrong marker", `{"schema_version":"wrong","collections":{}}`, "schema_version", false},
{"missing entries", `{"schema_version":"maximumdirect.glossary.v1","collections":{"terms":{}}}`, "entries", false},
{"invalid name", `{"schema_version":"maximumdirect.glossary.v1","collections":{"terms":{"entries":[{"id":"one","name":3,"category":"number"}]}}}`, "name", false},
{"invalid aliases", `{"schema_version":"maximumdirect.glossary.v1","collections":{"terms":{"entries":[{"id":"one","name":"One","category":"number","aliases":3}]}}}`, "aliases", false},
} {
t.Run(tc.name, func(t *testing.T) {
var document, before map[string]any
if err := json.Unmarshal([]byte(tc.document), &document); err != nil {
t.Fatal(err)
}
if err := json.Unmarshal([]byte(tc.document), &before); err != nil {
t.Fatal(err)
}
err := validator.Validate(document, "example.yml")
if (err == nil) != tc.valid {
t.Fatalf("Validate() = %v", err)
}
if !tc.valid && (!strings.Contains(err.Error(), "example.yml") || !strings.Contains(err.Error(), tc.location)) {
t.Fatalf("error lacks source or field location: %v", err)
}
if !reflect.DeepEqual(document, before) {
t.Fatal("validation modified the document")
}
})
}
}
func TestBundledExamples(t *testing.T) {
validator, err := NewValidator()
if err != nil {
t.Fatal(err)
}
for _, name := range []string{"glossary.yml", "sports.yml", "weather.yml", "weather.json"} {
t.Run(name, func(t *testing.T) {
path := filepath.Join("assets", "glossary", "v0.2.0", "examples", name)
data, err := os.ReadFile(path)
if err != nil {
t.Fatal(err)
}
var document map[string]any
if err := yaml.Unmarshal(data, &document); err != nil {
t.Fatal(err)
}
if err := validator.Validate(document, path); err != nil {
t.Fatal(err)
}
})
}
}
func TestOfflineLoader(t *testing.T) {
if _, err := (offlineLoader{}).Load("https://example.invalid/schema.json"); err == nil {
t.Fatal("unregistered resource was accepted")
}
}