ppx_wiretype

[@@deriving wiretype]: a type's wiretype description, derived

A newer version is out: ppx_wiretype 0.7.1

wiretype

ci release license OCaml 5.5+ Hypha Stack

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.

This is the README of 0.7.1, the latest version. This version's README