119 lines
3.6 KiB
Go
119 lines
3.6 KiB
Go
package validation
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"io/fs"
|
|
"sort"
|
|
"strings"
|
|
|
|
"github.com/santhosh-tekuri/jsonschema/v6"
|
|
)
|
|
|
|
const schemaBase = "https://forge.maximumdirect.net/eric/schemas/raw/tag/"
|
|
|
|
var contractNames = []string{"campaign", "glossary", "party"}
|
|
|
|
// Set is a compiled, offline schema set loaded from an unpacked distribution.
|
|
type Set struct {
|
|
schemas map[string]*jsonschema.Schema
|
|
}
|
|
|
|
// Load compiles every contract, including meta-schema validation, using only
|
|
// locally registered resources and the library's built-in dialect definitions.
|
|
func Load(root fs.FS) (*Set, error) {
|
|
version, err := fs.ReadFile(root, "VERSION")
|
|
if err != nil {
|
|
return nil, fmt.Errorf("read VERSION: %w", err)
|
|
}
|
|
compiler := jsonschema.NewCompiler()
|
|
compiler.DefaultDraft(jsonschema.Draft2020)
|
|
compiler.UseLoader(offlineLoader{})
|
|
ids := make(map[string]string)
|
|
for _, name := range contractNames {
|
|
file := "schemas/" + name + ".schema.json"
|
|
data, err := fs.ReadFile(root, file)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("%s: %w", file, err)
|
|
}
|
|
if !json.Valid(data) {
|
|
return nil, fmt.Errorf("%s: expected JSON syntax", file)
|
|
}
|
|
value, err := Decode(data)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("%s: %w", file, err)
|
|
}
|
|
doc, ok := value.(map[string]any)
|
|
if !ok {
|
|
return nil, fmt.Errorf("%s: expected a schema object", file)
|
|
}
|
|
id := schemaBase + strings.TrimSpace(string(version)) + "/" + file
|
|
if doc["$schema"] != "https://json-schema.org/draft/2020-12/schema" || doc["$id"] != id {
|
|
return nil, fmt.Errorf("%s: expected Draft 2020-12 and $id %q", file, id)
|
|
}
|
|
if err := compiler.AddResource(id, value); err != nil {
|
|
return nil, fmt.Errorf("%s: %w", file, err)
|
|
}
|
|
ids[name] = id
|
|
}
|
|
set := &Set{schemas: make(map[string]*jsonschema.Schema)}
|
|
for _, name := range contractNames {
|
|
schema, err := compiler.Compile(ids[name])
|
|
if err != nil {
|
|
return nil, fmt.Errorf("compile %s: %w", name, err)
|
|
}
|
|
set.schemas["maximumdirect."+name+".v1"] = schema
|
|
}
|
|
return set, nil
|
|
}
|
|
|
|
type offlineLoader struct{}
|
|
|
|
func (offlineLoader) Load(url string) (any, error) {
|
|
return nil, fmt.Errorf("schema resource %q is absent from the local distribution; network loading is disabled", url)
|
|
}
|
|
|
|
// Validate checks structure, then the selected semantic rules. It does not
|
|
// resolve imports or references, populate defaults, or mutate the input.
|
|
func (s *Set) Validate(value any) error {
|
|
doc, ok := value.(map[string]any)
|
|
if !ok {
|
|
return fmt.Errorf("structure: /: expected a document object")
|
|
}
|
|
version, ok := doc["schema_version"].(string)
|
|
if !ok {
|
|
return fmt.Errorf("structure: /schema_version: expected a contract identifier string")
|
|
}
|
|
schema := s.schemas[version]
|
|
if schema == nil {
|
|
return fmt.Errorf("structure: /schema_version: unsupported contract %q", version)
|
|
}
|
|
if err := schema.Validate(value); err != nil {
|
|
return fmt.Errorf("structure: %w", err)
|
|
}
|
|
if version == "maximumdirect.glossary.v1" {
|
|
return uniqueGlossaryIDs(doc["collections"].(map[string]any))
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func uniqueGlossaryIDs(collections map[string]any) error {
|
|
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)
|
|
seen := make(map[string]int, len(entries))
|
|
for i, entry := range entries {
|
|
id := entry.(map[string]any)["id"].(string)
|
|
if previous, exists := seen[id]; exists {
|
|
return fmt.Errorf("semantic: /collections/%s/entries/%d/id: duplicate ID %q (first at entry %d)", escape(name), i, id, previous)
|
|
}
|
|
seen[id] = i
|
|
}
|
|
}
|
|
return nil
|
|
}
|