Module itinera/schema

Zod, which itinera's type tokens are built on, for an application's own schemas.

Import z from here rather than from zod, so that the application and itinera share one copy of Zod: an ESM application importing zod itself loads a second copy beside the one itinera loads. A schema from any copy of Zod, or from any other library that implements Standard Schema, is accepted wherever a request names its type.

Zod's declarations name the global URL, so a project compiles them with Node's types or the DOM library, or with skipLibCheck.

Everything z holds is documented by Zod.

import assert from "node:assert/strict";
import { z } from "itinera/schema";
import { input, type Outcome, Step, step } from "itinera/step";

const Order = z.object({ id: z.string(), total: z.number().nonnegative() });

@step()
class Ship extends Step {
constructor(@input("order", Order) readonly order: z.input<typeof Order>) {
super();
}

override run(): Outcome {
return this.succeed();
}
}

assert.ok(Order.safeParse({ id: "R-1", total: 40 }).success);
assert.ok(!Order.safeParse({ id: "R-1", total: -1 }).success);
assert.equal(new Ship({ id: "R-1", total: 40 }).order.id, "R-1");