Skip to content

Wiretype

JSON contracts for modern OCaml.

Source on GitHubDocumentation

About

Define your JSON once and share it across your backend, front end and AI agents. Your records, variants and recursive types are the contract, their bounds and docs included.

Invalid input is refused with every mistake at once, each at its path with a code a client can act on. One walk prints JSON Schema, Zod and a Melange module, held to the server's verdict on every test run, so a form finds what is wrong before it sends.

Data contracts

Install

From open-opam, on OCaml 5.5 or later:

opam repo add open-opam https://github.com/open-opam/opam-repository.git
opam install wiretype ppx_wiretype

Then in your dune file:

(libraries wiretype)
(preprocess (pps ppx_wiretype))

In code

  1. A type, its bounds and its docs, derived as one description.

    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. *)
    
    Wiretype.decode line_json {|{"sku": "A1", "quantity": 120, "note": 7}|}
  2. Every problem in one pass, each with its path and a code a client branches on.

    quantity: This must be at most 99. (too_large)
    note: This must be text, not a number. (unexpected_type)
  3. The same description, printed as Zod for the front end.

    /** A line of an order. */
    export const LineSchema = z.object({
      sku: z.string(),
      /** how many */
      quantity: z.int().check(z.gte(1), z.lte(99)),
      note: z.optional(z.string().check(z.maxLength(200))),
    });
    export type Line = z.infer<typeof LineSchema>;

Build with Hypha.

  • Talk to us

    Bring an idea, a product or a system. Questions and feedback are just as welcome.

  • Services

    A new product built to last, or the system your business runs on rebuilt. Hypha's expertise and its foundation, end to end.

  • The Factory

    Make the decisions and let Hypha Autonomous Factory do the rest, turning them into a tested release, ready to ship.