payload-contract

command
v0.9.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 23, 2026 License: MIT Imports: 4 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

payload-contract - shared schema across languages (PoC)

Proof of ergonomics for the payload contract (AE-042). One shared JSON Schema, schemas/measurement.schema.json, used on both sides of a boundary in two languages:

  • driver (Go) produces typed Measurement values and casts them to the BFF.
  • bff (TS) validates every incoming measurement against the same schema with decode() at its boundary, before it counts. A valid measurement passes as a typed value; a malformed one is rejected with a clear reason and never pollutes state.

The Measurement type on both sides is generated from the schema (see Codegen), so the producer and consumer types cannot drift from the contract.

The runtime is untouched: the payload stays untyped on the wire, and validation is an opt-in call the BFF makes at the boundary it owns - not something the transport enforces.

Run

export GOTOOLCHAIN=local
# from the repo root
mise exec go@latest -- go build -o bin/aether ./cmd/aether
mise exec go@latest -- go build -o bin/contract-driver ./examples/payload-contract
bun install

cd examples/payload-contract
../../bin/aether up -f aether.toml

Expected output (the driver sends two valid measurements and one malformed one):

bff: accepted voltage=231.4V from s-1
bff: accepted current=12.5A from s-2
bff: rejected a malformed measurement - /metric: must be equal to one of the allowed values

Test

cd examples/payload-contract
bun test           # decode() accepts a valid measurement and rejects malformed ones, via the schema file

HTTP edge boundary

The manifest also exposes an HTTP edge that validates POSTed measurements against the same schema before they reach the ether - a malformed body never enters the system. With the demo running:

# valid measurement -> 202, cast to the bff
curl -i -X POST -H 'Content-Type: application/json' \
  -d '{"siteId":"s-9","metric":"current","value":10.0,"ts":1}' \
  http://127.0.0.1:7391/ingest

# malformed measurement -> 400 at the edge, naming the offending field
curl -i -X POST -H 'Content-Type: application/json' \
  -d '{"siteId":"s-9","metric":"pressure","value":10.0,"ts":1}' \
  http://127.0.0.1:7391/ingest
# HTTP/1.1 400 Bad Request
# payload does not match schema: /metric: value must be one of 'voltage', 'current', 'temperature'

The route opts in with a schema field (aether.toml); a route without one keeps the plain valid-JSON check. It is the same contract as the driver and bff - a third consumer, not a new mechanism.

Codegen

The native types live in gen/, generated from schemas/:

schemas/measurement.schema.json     # source of truth
gen/go/measurement.go               # generated Go struct  (package contract)
gen/ts/measurement.ts               # generated TS interface

gen/ is a derived, committed artifact - do not edit it by hand. After changing a schema, regenerate and commit:

cd examples/payload-contract
./codegen.sh          # go-jsonschema + json-schema-to-typescript, versions pinned in the script

CI enforces this: the codegen-drift job regenerates into a temp dir and diffs against the committed gen/, so a schema change that is not followed by a regeneration fails the build. Generating for other languages (e.g. a Python dataclass) is added when a consumer in that language needs it.

Documentation

Overview

Driver = a Go producer thrall. It is built against the same measurement.schema.json the BFF validates with: it constructs typed Measurement values and casts them to the BFF. To show the boundary at work it also sends one deliberately malformed payload, which the BFF rejects.

The producer is trusted (its values are already typed), so it does not validate before sending - validation belongs at the consumer's boundary. See PAYLOAD-CONTRACT.md (AE-042).

Directories

Path Synopsis
gen
go

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL