ZDDC/zddc/internal/handler/schemahandler.go
ZDDC 9b9d823a67 feat(zddc): .zddc JSON Schema (machine grammar) with structure/option tiers
Authoritative machine form of the GRAMMAR.md grammar: zddc.schema.json
(draft 2020-12) describes every .zddc key with type, enum, description, and
x-zddc-tier — "structure" (the project shape an end user shouldn't change:
paths, worm, *_tool, views, available_tools, auto_own*, party_source, history*,
records, acl, created_by) vs "option" (the blanks an operator fills: roles
members, field_codes, convert, display, admins, title, planned dates). This is
the contract a future .zddc form view uses to render option fields editable and
structure fields read-only.

Embedded (ZddcSchemaBytes) and served at GET /.api/zddc-schema for the client.
Test locks the tier classification.

Scope note: the schema uses $ref (recursive paths:) + patternProperties, which
the in-tree internal/jsonschema validator doesn't support — so it drives the
form/client now; wiring it as the SERVER validator (replacing validate.go's
hand-rolled checks) needs a $ref-capable validator and is a separate decision.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-05 14:54:07 -05:00

28 lines
973 B
Go

package handler
import (
"net/http"
"codeberg.org/VARASYS/ZDDC/zddc/internal/zddc"
)
// ZddcSchemaPath is the JSON endpoint serving the .zddc JSON Schema (the machine
// grammar). The browse client + the .zddc form view fetch it to drive editing
// (per-property x-zddc-tier marks structure vs option) and validation.
const ZddcSchemaPath = "/.api/zddc-schema"
// ServeZddcSchema returns the embedded .zddc JSON Schema. Read-only, no auth —
// it documents the policy grammar and leaks nothing. GET/HEAD only.
func ServeZddcSchema(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet && r.Method != http.MethodHead {
w.Header().Set("Allow", "GET, HEAD")
http.Error(w, "Method Not Allowed", http.StatusMethodNotAllowed)
return
}
w.Header().Set("Content-Type", "application/schema+json; charset=utf-8")
w.Header().Set("Cache-Control", "max-age=300")
if r.Method == http.MethodHead {
return
}
_, _ = w.Write(zddc.ZddcSchemaBytes())
}