2026-11-28 19:44:30 +00:00
|
|
|
package validation
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"io/fs"
|
|
|
|
|
"os"
|
|
|
|
|
"path/filepath"
|
|
|
|
|
"reflect"
|
|
|
|
|
"strings"
|
|
|
|
|
"testing"
|
|
|
|
|
"testing/fstest"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func loadSet(t *testing.T) *Set {
|
|
|
|
|
t.Helper()
|
|
|
|
|
set, err := Load(os.DirFS("../.."))
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
return set
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestDocuments(t *testing.T) {
|
|
|
|
|
set := loadSet(t)
|
2026-11-29 04:35:26 +00:00
|
|
|
directories := []string{"tests/valid", "tests/invalid"}
|
|
|
|
|
for _, record := range set.Catalog.Schemas {
|
|
|
|
|
directories = append(directories, filepath.Dir(record.Path)+"/examples")
|
|
|
|
|
}
|
|
|
|
|
for _, directory := range directories {
|
2026-11-28 19:44:30 +00:00
|
|
|
err := filepath.WalkDir(filepath.Join("../..", directory), func(path string, entry fs.DirEntry, walkErr error) error {
|
|
|
|
|
if walkErr != nil {
|
|
|
|
|
return walkErr
|
|
|
|
|
}
|
|
|
|
|
if entry.IsDir() {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
t.Run(filepath.ToSlash(path), func(t *testing.T) {
|
|
|
|
|
data, err := os.ReadFile(path)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
value, err := Decode(data)
|
|
|
|
|
var before []byte
|
|
|
|
|
if err == nil {
|
|
|
|
|
before, err = json.Marshal(value)
|
|
|
|
|
}
|
|
|
|
|
if err == nil {
|
2026-11-29 04:35:26 +00:00
|
|
|
err = validateFixture(t, set, value)
|
2026-11-28 19:44:30 +00:00
|
|
|
}
|
|
|
|
|
if directory == "tests/invalid" {
|
|
|
|
|
phase, _, _ := strings.Cut(entry.Name(), "-")
|
|
|
|
|
if err == nil || !strings.HasPrefix(err.Error(), phase+":") {
|
|
|
|
|
t.Fatalf("want %s failure, got %v", phase, err)
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
after, err := json.Marshal(value)
|
|
|
|
|
if err != nil || string(before) != string(after) {
|
|
|
|
|
t.Fatalf("validation mutated value: %s -> %s, %v", before, after, err)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
return nil
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestDefinedFieldShapes(t *testing.T) {
|
|
|
|
|
set := loadSet(t)
|
|
|
|
|
tests := []struct {
|
|
|
|
|
name string
|
|
|
|
|
body string
|
|
|
|
|
valid bool
|
|
|
|
|
}{
|
|
|
|
|
{"collection null", `collections: {weather: null}`, false},
|
|
|
|
|
{"entries omitted", `collections: {weather: {description: context}}`, false},
|
|
|
|
|
{"entries null", `collections: {weather: {entries: null}}`, false},
|
|
|
|
|
{"entries empty", `collections: {weather: {entries: []}}`, true},
|
|
|
|
|
{"aliases omitted", `collections: {weather: {entries: [{id: wind, name: Wind, category: speed}]}}`, true},
|
|
|
|
|
{"aliases null", `collections: {weather: {entries: [{id: wind, name: Wind, category: speed, aliases: null}]}}`, true},
|
|
|
|
|
{"aliases empty", `collections: {weather: {entries: [{id: wind, name: Wind, category: speed, aliases: []}]}}`, true},
|
|
|
|
|
{"aliases populated", `collections: {weather: {entries: [{id: wind, name: Wind, category: speed, aliases: [Breeze]}]}}`, true},
|
|
|
|
|
{"aliases invalid item", `collections: {weather: {entries: [{id: wind, name: Wind, category: speed, aliases: [1]}]}}`, false},
|
|
|
|
|
{"empty ID", `collections: {weather: {entries: [{id: "", name: Wind, category: speed}]}}`, false},
|
|
|
|
|
{"category required", `collections: {weather: {entries: [{id: wind, name: Wind}]}}`, false},
|
|
|
|
|
{"empty collection name", `collections: {"": {entries: []}}`, false},
|
|
|
|
|
{"provenance empty", `collections: {weather: {entries: [], provenance: {}}}`, true},
|
|
|
|
|
{"provenance absent metadata", `collections: {weather: {entries: [], provenance: {version: "", url: "", license: ""}}}`, true},
|
|
|
|
|
{"provenance extension", `collections: {weather: {entries: [], provenance: {title: Sample, source_id: 2}}}`, true},
|
|
|
|
|
{"provenance wrong known field", `collections: {weather: {entries: [], provenance: {ruleset: []}}}`, false},
|
|
|
|
|
{"unknown fields have no instruction semantics", `collections: {weather: {entries: [], instruction: {arbitrary: data}}}`, true},
|
|
|
|
|
{"null plural", `collections: {weather: {entries: [{id: wind, name: Wind, category: speed, plural: null}]}}`, true},
|
|
|
|
|
{"empty plural", `collections: {weather: {entries: [{id: wind, name: Wind, category: speed, plural: ""}]}}`, false},
|
|
|
|
|
}
|
|
|
|
|
for _, test := range tests {
|
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
|
|
|
|
value, err := Decode([]byte("schema_version: maximumdirect.glossary.v1\n" + test.body))
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
2026-11-29 04:35:26 +00:00
|
|
|
if err := validateFixture(t, set, value); (err == nil) != test.valid {
|
2026-11-28 19:44:30 +00:00
|
|
|
t.Fatalf("valid=%v, error=%v", test.valid, err)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
for _, test := range []struct {
|
|
|
|
|
body string
|
|
|
|
|
valid bool
|
|
|
|
|
}{
|
2026-11-29 04:29:11 +00:00
|
|
|
{"schema_version: maximumdirect.rpg.party.v1\ncharacters: null", false},
|
|
|
|
|
{"schema_version: maximumdirect.rpg.party.v1\ncharacters: {mira: {character: {}}}", false},
|
|
|
|
|
{"schema_version: maximumdirect.rpg.party.v1\ncharacters: {mira: {character: {name: Mira}, player: {}}}", false},
|
|
|
|
|
{"schema_version: maximumdirect.rpg.party.v1\ncharacters: {mira: {character: {name: Mira, classes: null}}}", false},
|
|
|
|
|
{"schema_version: maximumdirect.rpg.party.v1\ncharacters: {mira: {character: {name: Mira, classes: [{name: scout, level: 0}]}}}", false},
|
|
|
|
|
{"schema_version: maximumdirect.rpg.party.v1\ncharacters: {mira: {character: {name: Mira, classes: [{name: scout, level: 1.5}]}}}", false},
|
|
|
|
|
{"schema_version: maximumdirect.rpg.party.v1\ncharacters: {mira: {character: {name: Mira, classes: [{name: scout, level: 1.0000000000000000000001}]}}}", false},
|
|
|
|
|
{"schema_version: maximumdirect.rpg.campaign.v1\nid: example\nreferences: {}", true},
|
|
|
|
|
{"schema_version: maximumdirect.rpg.campaign.v1\nid: example\nreferences: {glossaries: []}", true},
|
|
|
|
|
{"schema_version: maximumdirect.rpg.campaign.v1\nid: example\nreferences: {party: null}", false},
|
|
|
|
|
{"schema_version: maximumdirect.rpg.campaign.v1\nid: example\nreferences: {glossaries: [false]}", false},
|
|
|
|
|
{"schema_version: maximumdirect.rpg.campaign.v1\nid: example\nreferences: null", false},
|
|
|
|
|
{"schema_version: maximumdirect.rpg.campaign.v1\nid: null", false},
|
|
|
|
|
{"schema_version: maximumdirect.rpg.campaign.v1\nname: Example", false},
|
2026-11-28 19:44:30 +00:00
|
|
|
} {
|
|
|
|
|
t.Run(test.body, func(t *testing.T) {
|
|
|
|
|
value, err := Decode([]byte(test.body))
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
2026-11-29 04:35:26 +00:00
|
|
|
if err := validateFixture(t, set, value); (err == nil) != test.valid {
|
2026-11-28 19:44:30 +00:00
|
|
|
t.Fatalf("valid=%v, error=%v", test.valid, err)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestDecodeProfile(t *testing.T) {
|
|
|
|
|
for _, input := range []string{
|
|
|
|
|
"a: 1\na: 2", "a: {b: 1, b: 2}", `{"a":1,"a":2}`,
|
|
|
|
|
"---\na: 1\n---\na: 2", "1: value", "true: value", "? [a, b]\n: value",
|
|
|
|
|
"a: !custom value", "a: !!binary Zm9v", "a: !!set {x: null}", "a: !!omap [{x: 1}]",
|
|
|
|
|
"a: &cycle [*cycle]", "a: *unknown", "a: .inf", "a: -.Inf", "a: .NaN",
|
|
|
|
|
"a: !!float true", "a: !!int false", "a: [broken", "a: &x {b: 1}\nc: {<<: *x}",
|
2026-11-28 23:25:12 +00:00
|
|
|
`a: !!float ""`, "a: !!float e3", "a: !!float +", "a: !!float .",
|
2026-11-28 19:44:30 +00:00
|
|
|
} {
|
|
|
|
|
t.Run(input, func(t *testing.T) {
|
|
|
|
|
if _, err := Decode([]byte(input)); err == nil || !strings.HasPrefix(err.Error(), "decode:") {
|
|
|
|
|
t.Fatalf("expected decoding failure, got %v", err)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
value, err := Decode([]byte(`
|
|
|
|
|
date: 2026-11-28
|
|
|
|
|
timestamp: 2026-11-28T12:30:00Z
|
|
|
|
|
affirmative: yes
|
|
|
|
|
switch: on
|
|
|
|
|
enabled: true
|
|
|
|
|
missing: ~
|
|
|
|
|
quoted: "true"
|
|
|
|
|
quoted_number: "1e500"
|
|
|
|
|
tagged_string: !!str 1e500
|
|
|
|
|
"<<": literal extension
|
|
|
|
|
original: &items [one, two]
|
|
|
|
|
copy: *items
|
|
|
|
|
`))
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
want := map[string]any{
|
|
|
|
|
"date": "2026-11-28", "timestamp": "2026-11-28T12:30:00Z",
|
|
|
|
|
"affirmative": "yes", "switch": "on", "enabled": true, "missing": nil, "quoted": "true",
|
|
|
|
|
"quoted_number": "1e500", "tagged_string": "1e500",
|
|
|
|
|
"<<": "literal extension", "original": []any{"one", "two"}, "copy": []any{"one", "two"},
|
|
|
|
|
}
|
|
|
|
|
if !reflect.DeepEqual(value, want) {
|
|
|
|
|
t.Fatalf("got %#v, want %#v", value, want)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-28 23:25:12 +00:00
|
|
|
func TestJSONDecoding(t *testing.T) {
|
2026-11-29 04:29:11 +00:00
|
|
|
value, err := Decode([]byte(`{"schema_version":"maximumdirect.rpg.campaign.v1","id":"weather","name":"Weather \ud83c\udf27","observations":[{"amount":0.123456789012345678901,"scale":1e500,"available":true,"missing":null}]}`))
|
2026-11-28 23:25:12 +00:00
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
want := map[string]any{
|
2026-11-29 04:29:11 +00:00
|
|
|
"schema_version": "maximumdirect.rpg.campaign.v1", "id": "weather", "name": "Weather 🌧",
|
2026-11-28 23:25:12 +00:00
|
|
|
"observations": []any{map[string]any{
|
|
|
|
|
"amount": json.Number("0.123456789012345678901"), "scale": json.Number("1e500"),
|
|
|
|
|
"available": true, "missing": nil,
|
|
|
|
|
}},
|
|
|
|
|
}
|
|
|
|
|
if !reflect.DeepEqual(value, want) {
|
|
|
|
|
t.Fatalf("JSON decoding changed data: got %#v, want %#v", value, want)
|
|
|
|
|
}
|
2026-11-29 04:35:26 +00:00
|
|
|
if err := validateFixture(t, loadSet(t), value); err != nil {
|
2026-11-28 23:25:12 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
for _, input := range []string{
|
|
|
|
|
`{"a":1,"\u0061":2}`,
|
|
|
|
|
`{"observations":[{"value":1,"value":2}]}`,
|
|
|
|
|
`{"\ud83c\udf27":1,"🌧":2}`,
|
|
|
|
|
} {
|
|
|
|
|
if _, err := Decode([]byte(input)); err == nil || !strings.Contains(err.Error(), "duplicate mapping key") {
|
|
|
|
|
t.Fatalf("expected duplicate-key failure for %s, got %v", input, err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-28 19:44:30 +00:00
|
|
|
func TestNumericPrecision(t *testing.T) {
|
|
|
|
|
for input, want := range map[string]string{
|
|
|
|
|
"1234567890123456789012345678901234567890": "1234567890123456789012345678901234567890",
|
|
|
|
|
"0.123456789012345678901234567890": "0.123456789012345678901234567890",
|
|
|
|
|
".5": "0.5", "5.": "5.0", "+.5": "0.5", "-0.5": "-0.5", "1e500": "1e500",
|
|
|
|
|
"0x10": "16", "0o10": "8", "0b10": "2", "010": "8", "1_000": "1000",
|
2026-11-28 23:25:12 +00:00
|
|
|
`!!float "0"`: "0", "!!float .5": "0.5", "!!float 5.": "5.0", "!!float 1e500": "1e500",
|
2026-11-28 19:44:30 +00:00
|
|
|
} {
|
|
|
|
|
t.Run(input, func(t *testing.T) {
|
|
|
|
|
value, err := Decode([]byte("measurement: " + input))
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
number, ok := value.(map[string]any)["measurement"].(json.Number)
|
|
|
|
|
if !ok || number.String() != want {
|
|
|
|
|
t.Fatalf("want exact JSON number %q, got %#v", want, value)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestYAMLJSONEquivalence(t *testing.T) {
|
2026-11-29 04:35:26 +00:00
|
|
|
yamlBytes, err := os.ReadFile("../../glossary/v0.1.0/examples/weather.yml")
|
2026-11-28 19:44:30 +00:00
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
2026-11-29 04:35:26 +00:00
|
|
|
jsonBytes, err := os.ReadFile("../../glossary/v0.1.0/examples/weather.json")
|
2026-11-28 19:44:30 +00:00
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
yamlValue, err := Decode(yamlBytes)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
jsonValue, err := Decode(jsonBytes)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
if !reflect.DeepEqual(yamlValue, jsonValue) {
|
|
|
|
|
t.Fatal("YAML and JSON represent different domain data")
|
|
|
|
|
}
|
|
|
|
|
data, err := json.Marshal(yamlValue)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
roundTrip, err := Decode(data)
|
|
|
|
|
if err != nil || !reflect.DeepEqual(yamlValue, roundTrip) {
|
|
|
|
|
t.Fatalf("JSON conversion changed data: %v", err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-29 04:35:26 +00:00
|
|
|
// validateFixture chooses the known initial definition for domain-focused fixtures.
|
|
|
|
|
// Public CLI selection is always explicit and tested separately.
|
|
|
|
|
func validateFixture(t *testing.T, set *Set, value any) error {
|
|
|
|
|
t.Helper()
|
|
|
|
|
selection := "glossary@0.1.0"
|
|
|
|
|
if doc, ok := value.(map[string]any); ok {
|
|
|
|
|
switch doc["schema_version"] {
|
|
|
|
|
case "maximumdirect.rpg.party.v1":
|
|
|
|
|
selection = "rpg/party@0.1.0"
|
|
|
|
|
case "maximumdirect.rpg.campaign.v1":
|
|
|
|
|
selection = "rpg/campaign@0.1.0"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
schema, err := set.Select(selection)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
return schema.Validate(value)
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-28 19:44:30 +00:00
|
|
|
func TestDistributionAndOfflineResolution(t *testing.T) {
|
|
|
|
|
files := fstest.MapFS{}
|
2026-11-29 04:35:26 +00:00
|
|
|
root := os.DirFS("../..")
|
|
|
|
|
catalogBytes, err := fs.ReadFile(root, "catalog.json")
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
var catalog Catalog
|
|
|
|
|
if err := json.Unmarshal(catalogBytes, &catalog); err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
files["catalog.json"] = &fstest.MapFile{Data: catalogBytes}
|
|
|
|
|
for _, record := range catalog.Schemas {
|
|
|
|
|
data, err := fs.ReadFile(root, record.Path)
|
2026-11-28 19:44:30 +00:00
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
2026-11-29 04:35:26 +00:00
|
|
|
files[record.Path] = &fstest.MapFile{Data: data}
|
2026-11-28 19:44:30 +00:00
|
|
|
}
|
2026-11-29 04:35:26 +00:00
|
|
|
partyPath := "rpg/party/v0.1.0/schema.json"
|
|
|
|
|
original := files[partyPath].Data
|
|
|
|
|
replacement := `"name": {"$ref": "` + schemaBase + `glossary/v0.1.0/schema.json#/$defs/text"}`
|
|
|
|
|
files[partyPath] = &fstest.MapFile{Data: []byte(strings.Replace(string(original), `"name": {"$ref": "#/$defs/text"}`, replacement, 1))}
|
2026-11-28 19:44:30 +00:00
|
|
|
set, err := Load(files)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
2026-11-29 04:35:26 +00:00
|
|
|
selected, err := set.Select("rpg/party@0.1.0")
|
2026-11-28 19:44:30 +00:00
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
2026-11-29 04:35:26 +00:00
|
|
|
value, err := Decode([]byte("schema_version: maximumdirect.rpg.party.v1\ncharacters: {mira: {character: {name: Mira}}}"))
|
|
|
|
|
if err != nil {
|
2026-11-28 19:44:30 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
2026-11-29 04:35:26 +00:00
|
|
|
if err := selected.Validate(value); err != nil {
|
|
|
|
|
t.Fatal(err)
|
2026-11-28 19:44:30 +00:00
|
|
|
}
|
2026-11-29 04:35:26 +00:00
|
|
|
files["VERSION"] = &fstest.MapFile{Data: []byte("2099.01.0\n")}
|
|
|
|
|
if _, err := Load(files); err != nil {
|
|
|
|
|
t.Fatalf("distribution version changed schema identities: %v", err)
|
|
|
|
|
}
|
|
|
|
|
for _, replacement := range []string{
|
|
|
|
|
`"name": {"$ref": "https://example.invalid/missing.json"}`,
|
|
|
|
|
`"name": {"type": 123}`,
|
|
|
|
|
`"name": {"$ref": "https://schemas.maximumdirect.net/glossary/v9.0.0/schema.json"}`,
|
|
|
|
|
} {
|
|
|
|
|
files[partyPath] = &fstest.MapFile{Data: []byte(strings.Replace(string(original), `"name": {"$ref": "#/$defs/text"}`, replacement, 1))}
|
|
|
|
|
if _, err := Load(files); err == nil {
|
|
|
|
|
t.Fatal("malformed schema or absent reference compiled")
|
|
|
|
|
}
|
2026-11-28 19:44:30 +00:00
|
|
|
}
|
2026-11-29 04:35:26 +00:00
|
|
|
files[partyPath] = &fstest.MapFile{Data: []byte("title: not JSON")}
|
2026-11-28 19:44:30 +00:00
|
|
|
if _, err := Load(files); err == nil {
|
2026-11-29 04:35:26 +00:00
|
|
|
t.Fatal("schema accepted non-JSON syntax")
|
2026-11-28 19:44:30 +00:00
|
|
|
}
|
|
|
|
|
}
|