Skip to content
hyphatechPublic

Repository files navigation

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 hypha https://github.com/hyphatech/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.

Contributing

See AGENTS.md.

Licence

MIT, copyright Hypha Technologies Ltd. See LICENSE.

Releases

Packages

Contributors

Languages