Establish shared domain schemas and offline validation
This commit is contained in:
62
cmd/validate/main.go
Normal file
62
cmd/validate/main.go
Normal file
@@ -0,0 +1,62 @@
|
||||
// validate checks completed domain documents against a local schema distribution.
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"forge.maximumdirect.net/eric/schemas/internal/validation"
|
||||
)
|
||||
|
||||
func main() { os.Exit(run(os.Args[1:], os.Stdout, os.Stderr)) }
|
||||
|
||||
func run(args []string, stdout, stderr io.Writer) int {
|
||||
flags := flag.NewFlagSet("validate", flag.ContinueOnError)
|
||||
flags.SetOutput(stderr)
|
||||
root := flags.String("root", ".", "unpacked schema-set root (contains VERSION and schemas/)")
|
||||
flags.Usage = func() {
|
||||
fmt.Fprintln(stderr, "Usage: validate [-root DIRECTORY] DOCUMENT...")
|
||||
flags.PrintDefaults()
|
||||
}
|
||||
if err := flags.Parse(args); err != nil {
|
||||
if err == flag.ErrHelp {
|
||||
return 0
|
||||
}
|
||||
return 2
|
||||
}
|
||||
if flags.NArg() == 0 {
|
||||
flags.Usage()
|
||||
return 2
|
||||
}
|
||||
set, err := validation.Load(os.DirFS(*root))
|
||||
if err != nil {
|
||||
fmt.Fprintf(stderr, "schema set: %v\n", err)
|
||||
return 2
|
||||
}
|
||||
status := 0
|
||||
for _, file := range flags.Args() {
|
||||
data, err := os.ReadFile(file)
|
||||
if err == nil && strings.EqualFold(filepath.Ext(file), ".json") && !json.Valid(data) {
|
||||
err = fmt.Errorf("decode: expected JSON syntax")
|
||||
}
|
||||
var value any
|
||||
if err == nil {
|
||||
value, err = validation.Decode(data)
|
||||
}
|
||||
if err == nil {
|
||||
err = set.Validate(value)
|
||||
}
|
||||
if err != nil {
|
||||
fmt.Fprintf(stderr, "%s: %v\n", file, err)
|
||||
status = 1
|
||||
continue
|
||||
}
|
||||
fmt.Fprintf(stdout, "%s: valid (%s)\n", file, value.(map[string]any)["schema_version"])
|
||||
}
|
||||
return status
|
||||
}
|
||||
48
cmd/validate/main_test.go
Normal file
48
cmd/validate/main_test.go
Normal file
@@ -0,0 +1,48 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestCommand(t *testing.T) {
|
||||
for _, test := range []struct {
|
||||
name string
|
||||
args []string
|
||||
code int
|
||||
out string
|
||||
err string
|
||||
}{
|
||||
{"valid", []string{"-root", "../..", "../../examples/party.yml"}, 0, "valid (maximumdirect.party.v1)", ""},
|
||||
{"continue after failure", []string{"-root", "../..", "../../tests/invalid/semantic-duplicate-id.yml", "../../examples/weather.json"}, 1, "weather.json: valid", "semantic:"},
|
||||
{"structural", []string{"-root", "../..", "../../tests/invalid/structure-missing-name.yml"}, 1, "", "structure:"},
|
||||
{"decode", []string{"-root", "../..", "../../tests/invalid/decode-duplicate-key.yml"}, 1, "", "decode:"},
|
||||
{"file missing", []string{"-root", "../..", filepath.Join(t.TempDir(), "missing.yml")}, 1, "", "missing.yml"},
|
||||
{"no documents", nil, 2, "", "Usage:"},
|
||||
{"bad flag", []string{"-unknown"}, 2, "", "flag provided"},
|
||||
{"help", []string{"-h"}, 0, "", "Usage:"},
|
||||
{"missing set", []string{"-root", t.TempDir(), "../../examples/party.yml"}, 2, "", "schema set:"},
|
||||
} {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
var stdout, stderr bytes.Buffer
|
||||
code := run(test.args, &stdout, &stderr)
|
||||
if code != test.code || !strings.Contains(stdout.String(), test.out) || !strings.Contains(stderr.String(), test.err) {
|
||||
t.Fatalf("exit=%d, stdout=%q, stderr=%q", code, &stdout, &stderr)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestJSONSyntax(t *testing.T) {
|
||||
file := filepath.Join(t.TempDir(), "document.json")
|
||||
if err := os.WriteFile(file, []byte("schema_version: maximumdirect.campaign.v1\nid: sample"), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var stdout, stderr bytes.Buffer
|
||||
if run([]string{"-root", "../..", file}, &stdout, &stderr) != 1 || !strings.Contains(stderr.String(), "decode: expected JSON syntax") {
|
||||
t.Fatalf("expected JSON syntax failure, got %q", &stderr)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user