Establish shared domain schemas and offline validation
This commit is contained in:
283
internal/validation/validation_test.go
Normal file
283
internal/validation/validation_test.go
Normal file
@@ -0,0 +1,283 @@
|
||||
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)
|
||||
for _, directory := range []string{"examples", "tests/valid", "tests/invalid"} {
|
||||
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 {
|
||||
err = set.Validate(value)
|
||||
}
|
||||
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)
|
||||
}
|
||||
if err := set.Validate(value); (err == nil) != test.valid {
|
||||
t.Fatalf("valid=%v, error=%v", test.valid, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
for _, test := range []struct {
|
||||
body string
|
||||
valid bool
|
||||
}{
|
||||
{"schema_version: maximumdirect.party.v1\ncharacters: null", false},
|
||||
{"schema_version: maximumdirect.party.v1\ncharacters: {mira: {character: {}}}", false},
|
||||
{"schema_version: maximumdirect.party.v1\ncharacters: {mira: {character: {name: Mira}, player: {}}}", false},
|
||||
{"schema_version: maximumdirect.party.v1\ncharacters: {mira: {character: {name: Mira, classes: null}}}", false},
|
||||
{"schema_version: maximumdirect.party.v1\ncharacters: {mira: {character: {name: Mira, classes: [{name: scout, level: 0}]}}}", false},
|
||||
{"schema_version: maximumdirect.party.v1\ncharacters: {mira: {character: {name: Mira, classes: [{name: scout, level: 1.5}]}}}", false},
|
||||
{"schema_version: maximumdirect.party.v1\ncharacters: {mira: {character: {name: Mira, classes: [{name: scout, level: 1.0000000000000000000001}]}}}", false},
|
||||
{"schema_version: maximumdirect.campaign.v1\nid: example\nreferences: {}", true},
|
||||
{"schema_version: maximumdirect.campaign.v1\nid: example\nreferences: {glossaries: []}", true},
|
||||
{"schema_version: maximumdirect.campaign.v1\nid: example\nreferences: {party: null}", false},
|
||||
{"schema_version: maximumdirect.campaign.v1\nid: example\nreferences: {glossaries: [false]}", false},
|
||||
{"schema_version: maximumdirect.campaign.v1\nid: example\nreferences: null", false},
|
||||
{"schema_version: maximumdirect.campaign.v1\nid: null", false},
|
||||
{"schema_version: maximumdirect.campaign.v1\nname: Example", false},
|
||||
} {
|
||||
t.Run(test.body, func(t *testing.T) {
|
||||
value, err := Decode([]byte(test.body))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := set.Validate(value); (err == nil) != test.valid {
|
||||
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}",
|
||||
} {
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
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",
|
||||
} {
|
||||
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) {
|
||||
yamlBytes, err := os.ReadFile("../../examples/weather.yml")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
jsonBytes, err := os.ReadFile("../../examples/weather.json")
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDistributionAndOfflineResolution(t *testing.T) {
|
||||
// Only the distributed schemas and version are needed; no checkout or HTTP
|
||||
// resource loader is available through this in-memory filesystem.
|
||||
files := fstest.MapFS{}
|
||||
for _, file := range []string{"VERSION", "schemas/campaign.schema.json", "schemas/glossary.schema.json", "schemas/party.schema.json"} {
|
||||
data, err := os.ReadFile(filepath.Join("../..", file))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
files[file] = &fstest.MapFile{Data: data}
|
||||
}
|
||||
set, err := Load(files)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
value, err := Decode([]byte("schema_version: maximumdirect.campaign.v1\nid: offline"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := set.Validate(value); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
original := files["schemas/party.schema.json"].Data
|
||||
t.Run("shared reference in the same release", func(t *testing.T) {
|
||||
version := strings.TrimSpace(string(files["VERSION"].Data))
|
||||
reference := `"name": {"$ref": "` + schemaBase + version + `/schemas/glossary.schema.json#/$defs/text"}`
|
||||
files["schemas/party.schema.json"] = &fstest.MapFile{Data: []byte(strings.Replace(string(original), `"name": {"$ref": "#/$defs/text"}`, reference, 1))}
|
||||
if _, err := Load(files); err != nil {
|
||||
t.Fatalf("bundled cross-schema reference failed offline: %v", err)
|
||||
}
|
||||
})
|
||||
for name, replacement := range map[string]string{
|
||||
"missing reference": `"name": {"$ref": "https://example.invalid/missing.json"}`,
|
||||
"bad schema keyword": `"name": {"type": 123}`,
|
||||
"mixed release": `"name": {"$ref": "https://forge.maximumdirect.net/eric/schemas/raw/tag/v0.0.0/schemas/glossary.schema.json"}`,
|
||||
} {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
files["schemas/party.schema.json"] = &fstest.MapFile{Data: []byte(strings.Replace(string(original), `"name": {"$ref": "#/$defs/text"}`, replacement, 1))}
|
||||
if _, err := Load(files); err == nil {
|
||||
t.Fatal("incomplete or malformed distribution compiled")
|
||||
}
|
||||
})
|
||||
}
|
||||
files["schemas/party.schema.json"] = &fstest.MapFile{Data: original}
|
||||
files["schemas/party.schema.json"] = &fstest.MapFile{Data: []byte("title: not JSON")}
|
||||
if _, err := Load(files); err == nil {
|
||||
t.Fatal("a schema file accepted non-JSON syntax")
|
||||
}
|
||||
files["schemas/party.schema.json"] = &fstest.MapFile{Data: original}
|
||||
files["VERSION"] = &fstest.MapFile{Data: []byte("v9.0.0\n")}
|
||||
if _, err := Load(files); err == nil {
|
||||
t.Fatal("schema IDs did not match the distribution's version")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user