Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 25 additions & 10 deletions .github/workflows/docs.yml
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
# The documentation site on GitHub Pages, rebuilt on every push to main:
# The documentation site on GitHub Pages: built on every pull request, which
# fails if agents/ is not what the pages write, and deployed from main.
# odoc's reference from the .mli files, then Starlight's pages around it.
name: docs

on:
push:
branches: [main]
pull_request:

# A build reads the repository and nothing more; the deploy job alone is
# given the rights to publish.
permissions:
contents: read
pages: write
id-token: write

# A newer push to a pull request replaces its build; main's deploys queue
# and always finish.
concurrency:
group: pages
cancel-in-progress: true
group: pages-${{ github.ref }}
cancel-in-progress: ${{ github.event_name == 'pull_request' }}

jobs:
deploy:
build:
runs-on: ubuntu-24.04
environment:
name: github-pages
url: ${{ steps.deployment.outputs.page_url }}
steps:
- uses: actions/configure-pages@v6
- uses: actions/checkout@v5
- uses: ocaml/setup-ocaml@v3
with:
Expand All @@ -38,8 +38,23 @@ jobs:
# the token, past its limit for a runner's shared address.
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# The build writes agents/ from the pages: a page changed without it
# differs here.
- run: git add --intent-to-add agents/ && git diff --exit-code agents/
- uses: actions/upload-pages-artifact@v5
with:
path: _build/docs
deploy:
needs: build
if: github.ref == 'refs/heads/main'
runs-on: ubuntu-24.04
permissions:
pages: write
id-token: write
environment:
name: github-pages
url: ${{ steps.deployment.outputs.page_url }}
steps:
- uses: actions/configure-pages@v6
- uses: actions/deploy-pages@v5
id: deployment
16 changes: 14 additions & 2 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,16 @@ make test # every suite, the kinds' rows through Zod, and the shared rows
# through the printed Melange module
make lint # formatting, odoc, and the release build
make fmt # format in place
make docs # the documentation site into _build/docs, as Pages publishes it; docs-serve serves it
make docs # the documentation site into _build/docs, as Pages publishes it, and
# agents/ from its pages; docs-serve serves it
make release VERSION=1.2.3 # once CHANGES.md names it: tag and release with open-opam
```

The Zod rows, the Melange rows and `make docs` need Bun 1.3 or later;
`make test-zod` runs the Zod rows alone. Melange is a test dependency,
installed with the rest. CI runs `make lint` and `make test` on OCaml 5.5.
`docs.yml` publishes the site to GitHub Pages on every push to `main`.
`docs.yml` builds the site on every pull request and publishes it to GitHub
Pages from `main`.

## Layout

Expand Down Expand Up @@ -52,6 +54,10 @@ docs/ the documentation site, a Starlight site in Hypha's look,
src/content/docs/, each importing the examples it shows
whole, as "@examples/<file>.ml?raw" into Starlight's
<Code>; built into _build/docs, which dune clean removes
agents/ the documentation for a user's agent, generated by
starlight-hypha-agents from the pages, `make docs`:
llms.txt, the catalogue; pages/, each page as
Markdown; installed by the wiretype package
examples/ the programs the site shows, and the modules they share,
each a whole file built with the rest
test/
Expand Down Expand Up @@ -171,6 +177,12 @@ break.
refusals. Symptom: `rg -n -w invalid_arg src ppx` finds it outside
`src/wiretype.ml`. A stdlib call that raises (`Char.chr`, `String.sub`)
is not found by a search, and is checked by reading.
- **`agents/` is generated, never edited.** starlight-hypha-agents writes
it from the documentation's pages and its options in
`docs/astro.config.mjs`: a page changed is `make docs` run in the same
commit. Why: a user's agent reads what the release installs, offline,
and a page it reads must be the release's. Test: the docs workflow builds
the pages on every pull request and fails if `agents/` then differs.
- **The opens are `Tuple`'s, inside `Tuple.( ... )` as its `.mli` says, and
the deriver's `Ppxlib` and `Ast_pattern`**, which every ppxlib rewriter
opens to read the AST it matches. Symptom:
Expand Down
3 changes: 3 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## Unreleased

- The wiretype package installs its documentation for agents: each page as
Markdown and `llms.txt`, at `$(opam var wiretype:doc)`. The README's *With an
agent* gives the line for a project's `AGENTS.md` that sends an agent there.
- wiretype is published to open-opam, `opam repo add open-opam
https://github.com/open-opam/opam-repository.git`, and no longer to Hypha's
opam repository.
Expand Down
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,18 @@ note: This must be text, not a number. (unexpected_type)
The [quick start](https://hyphatech.github.io/wiretype/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:

```md
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](AGENTS.md).
Expand Down
26 changes: 26 additions & 0 deletions agents/llms.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# wiretype

> JSON contracts for modern OCaml.

## Start here

- [Getting Started](pages/index.md): JSON contracts for modern OCaml.
- [Quick start](pages/quick-start.md)

## Guides

- [Descriptions by hand](pages/guide/by-hand.md)
- [The deriver, every attribute](pages/guide/deriver.md)
- [Reading and its problems](pages/guide/reading.md)
- [Writing](pages/guide/writing.md)
- [Bounds](pages/guide/bounds.md)
- [Kinds](pages/guide/kinds.md)
- [JSON Schema](pages/guide/json-schema.md)
- [Zod](pages/guide/zod.md)
- [Melange](pages/guide/melange.md)
- [Walking a description](pages/guide/walking.md)
- [What it does not do](pages/guide/what-it-does-not-do.md)

## Reference

- [Reference](pages/reference.md)
110 changes: 110 additions & 0 deletions agents/pages/guide/bounds.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
# Bounds

A bound is part of a description. It is checked while reading, refused on
writing, and stated in each schema the description prints -- ranges,
`multiple_of`, lengths and counts alike.

## The bounded descriptions

```ocaml title="bounded.ml"
module J = Wiretype

let quantity = J.int_bounded ~min:1 ~max:99 ()
let even = J.int_bounded ~multiple_of:2 ()
let price = J.number_bounded ~above:0. ~multiple_of:0.01 ()
let ratio = J.number_bounded ~min:0. ~below:1. ()
let name = J.string_bounded ~min_length:1 ~max_length:4 ()
let tags = J.list ~max_items:2 J.string
let scores = J.dict ~min_properties:1 ~key:J.string ~value:J.int ()
```

| Description | Bounds |
|---|---|
| `int_bounded`, `int64_bounded` | `~min`, `~max`, `~multiple_of` |
| `number_bounded` | `~min` and `~max`, at least and at most; `~above` and `~below`, greater and less than; `~multiple_of` |
| `string_bounded` | `~min_length`, `~max_length` |
| `list` | `~min_items`, `~max_items` |
| `dict` | `~min_properties`, `~max_properties` |

Derived, each is an attribute of the same name on the field:
`[@min 1]`, `[@above 0.]`, `[@max_length 200]`, `[@max_items 10]`, and
`[@min_properties]` on a `[@dict]`.

## What they read

```ocaml title="bounds.ml"
module J = Wiretype
module B = Bounded

let read name description text =
match J.decode description text with
| Ok _ -> Printf.printf "%-8s %s: read\n" name text
| Error problems ->
Printf.printf "%-8s %s: %s\n" name text
(J.Problem.list_to_string problems)

let () =
read "quantity" B.quantity "0";
read "quantity" B.quantity "1e1";
read "even" B.even "7";
read "price" B.price "19.99";
read "price" B.price "19.999";
read "ratio" B.ratio "1";
read "name" B.name {|"café"|};
read "name" B.name {|"cafés"|};
read "tags" B.tags {|["a", "b", "c"]|};
read "scores" B.scores "{}"
```

```console
quantity 0: the document: This must be at least 1. (too_small)
quantity 1e1: read
even 7: the document: This must be a multiple of 2. (not_a_multiple)
price 19.99: read
price 19.999: the document: This must be a multiple of 0.01. (not_a_multiple)
ratio 1: the document: This must be less than 1. (too_large)
name "café": read
name "cafés": the document: This must be at most 4 characters long. (too_long)
tags ["a", "b", "c"]: the document: This must have at most 2 items. (too_many)
scores {}: the document: This must have at least 1 member. (too_few)
```

- A length is in code points, as JSON Schema counts one, so `café` is
four long.
- `multiple_of` on an integer is exact. On a number it holds where the
quotient is a whole number to within a few rounding errors, so `19.99`
is a multiple of `0.01`, as the decimals written mean.
- An infinite bound every number meets -- `~max:infinity` -- is none.

## Said exactly in every schema

A bound is wiretype's to decide, and a printer says it exactly. Zod's
`multipleOf` decides a number's alike, and an integer's loosely, so an
integer's is printed as an exact `z.refine`. What a schema can say only
loosely is printed loosely and listed: an integer's bound past 2^53, which
a double holds only roughly, is left out and reported in
[`Schema.loose`](json-schema.md#loose-and-wrong).

## A bound no value meets

A bound no value meets, alone or with another -- a least above a most, a
negative length or count, a `multiple_of` that is not positive -- raises
`Invalid_argument` where the description is built, so the mistake is found
when the program starts:

```ocaml title="no_value.ml"
module J = Wiretype

let () =
match J.int_bounded ~min:10 ~max:1 () with
| (_ : int J.t) -> ()
| exception Invalid_argument message -> print_endline message
```

```console
Wiretype.int_bounded: min is 10 and max 1, which no integer meets
```

The deriver refuses a least and a most written as literals with no value
between them at compile time, and leaves a bound that means nothing alone
-- `[@multiple_of 0]` -- to the library.
Loading