wiretype
JSON contracts for modern OCaml
wiretype
JSON contracts for modern OCaml.
Strict, light and derived from your types. Built for JSON Schema, Zod and Melange.
Documentation: hyphatech.github.io/wiretype
Features
- Derived from your types: your records, variants and recursive types are the contract, their bounds and docs included.
- Every problem at once: invalid input is refused with every mistake, each at its path with a code a client can act on.
- JSON Schema, Zod and Melange: one walk prints all three, and the Zod and the Melange module are held to the server's verdict on every test run, so a form finds what is wrong before it sends.
Install
opam repo add open-opam https://github.com/open-opam/opam-repository.git
opam install wiretype ppx_wiretype
Add to your dune:
(libraries wiretype)
(preprocess (pps ppx_wiretype))
Example
type line = {
sku : string;
quantity : int; [@min 1] [@max 99] (** how many *)
note : string option; [@max_length 200]
}
[@@deriving wiretype]
(** A line of an order. *)
Reading a document that gets two members wrong:
Wiretype.decode line_json {|{"sku": "A1", "quantity": 120, "note": 7}|}
is Error with both problems, each printed by Problem.to_string as its
path, a sentence and its code:
quantity: This must be at most 99. (too_large)
note: This must be text, not a number. (unexpected_type)
The quick start runs it, and prints the same description as JSON Schema and Zod.
With an agent
Each release installs its documentation for an agent with it, at
$(opam var wiretype:doc): llms.txt, and every page in Markdown. An agent
reads it when your project's AGENTS.md tells it to:
Before writing code with wiretype, read `$(opam var wiretype:doc)/llms.txt` and
the pages it lists for the parts you need, and use those parts rather than
writing your own.
Contributing
See AGENTS.md.
Licence
MIT, copyright Hypha Technologies Ltd. See LICENSE.
| Package | Constraint |
|---|---|
| dune | >= "3.18" |
| ocaml | >= "5.5" |
| ptime | >= "1.2.0" |
| mtime | >= "2.1.0" |
| uuidm | >= "0.9.10" |
| alcotest | with-test & >= "1.9.1" |
| melange | with-test & >= "7.0.1-55" |
| odoc | with-dev-setup & >= "3.2.1" |
| merlin | with-dev-setup & >= "5.6-504" |
| ocaml-lsp-server | with-dev-setup & >= "1.25.0" |
| ocamlformat | with-dev-setup & = "0.29.0" |
| odoc | with-doc |
| Package | Constraint |
|---|---|
| open-opam | >= "0.7.0" & < "0.8.0" |
| ppx_wiretype | = version |
| solidtype | with-test & >= "0.7.0" & < "0.8.0" |
| spindle | >= "0.7.0" & < "0.8.0" |
| spindle-client | >= "0.7.0" & < "0.8.0" |
| spindle-http | >= "0.7.0" & < "0.8.0" |
| spindle-mcp | >= "0.7.0" & < "0.8.0" |
| spindle-mcp-client | >= "0.7.0" & < "0.8.0" |
| spindle-postgres | with-test & >= "0.7.0" & < "0.8.0" |
| spindle-sqlite | with-test & >= "0.7.0" & < "0.8.0" |
| spool | >= "0.7.0" & < "0.8.0" |
| spool-dashboard | >= "0.7.0" & < "0.8.0" |