Go SDK
Local validation of INHERIT estate documents in Go with embedded schemas and thread-safe compiled schema caching.
Installation#
bash
go get github.com/openinherit/sdk-goRequires Go 1.21 or later.
Quick start#
go
package main
import (
"fmt"
"os"
openinherit "github.com/openinherit/sdk-go"
)
func main() {
doc, _ := os.ReadFile("estate.json")
result, err := openinherit.Validate(doc)
if err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
if result.Valid {
fmt.Printf("Valid (Level %d)\n", result.ConformanceLevel)
} else {
for _, e := range result.Errors {
fmt.Printf(" %s: %s\n", e.Path, e.Message)
}
}
}API reference#
Validate(document []byte) (*ValidationResult, error)#
Validates an INHERIT document, auto-detecting estate or catalogue mode from the $schema field.
ValidateEstate(document []byte) (*ValidationResult, error)#
Validates a document as an estate document regardless of its $schema field.
ValidateCatalogue(document []byte) (*ValidationResult, error)#
Validates a document as a catalogue document regardless of its $schema field.
ValidationResult#
go
type ValidationResult struct {
Valid bool `json:"valid"`
SchemaMode string `json:"schemaMode"`
ConformanceLevel int `json:"conformanceLevel"`
Errors []ValidationError `json:"errors"`
Disclaimer string `json:"disclaimer"`
}
type ValidationError struct {
Path string `json:"path"`
Message string `json:"message"`
Level int `json:"level"`
}Conformance level constants#
go
const (
ConformanceFailed = 0 // Schema validation failed
ConformanceLevel1 = 1 // JSON Schema valid
ConformanceLevel2 = 2 // Schema + referential integrity valid
)Architecture#
The SDK embeds the bundled INHERIT v3 schemas as Go embed directives. Schemas are compiled once on first use (sync.Once) and reused across all subsequent calls — safe for concurrent use from multiple goroutines.
Examples#
Validate and serialise result to JSON#
go
result, err := openinherit.Validate(doc)
if err != nil {
log.Fatal(err)
}
out, _ := json.MarshalIndent(result, "", " ")
fmt.Println(string(out))Force estate mode#
go
result, err := openinherit.ValidateEstate(doc)HTTP handler#
go
func validateHandler(w http.ResponseWriter, r *http.Request) {
body, _ := io.ReadAll(r.Body)
result, err := openinherit.Validate(body)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(result)
}