Skip to content

json-schema-to-zod

@evolve-framework/json-schema-to-zod · v1.0.0

Turns the JSON Schemas of Evolve's events and notifications into TypeScript: a Zod validator and a matching type for each one, written to a single generated file. A package that publishes or consumes an event can then check the shape at runtime and get the type at compile time, both from the one schema in @evolve-framework/schemas.

When to use

  • A package publishes or handles an internal event or notification, and needs a validator and a type for it.
  • You run it from a codegen script, not at runtime: it reads the schemas, follows their $refs and writes a .ts file as biome check --write leaves it under your project's Biome configuration, so regenerating an unchanged schema reproduces the committed file.

Get started

  1. Install the package with the peer dependencies it needs. It comes from the Evolve registry, which needs a one-time sign-in.

    pnpm add @evolve-framework/json-schema-to-zod @evolve-framework/core
    Requirements2
    @evolve-framework/core>= 1.0.0 < 2.0.0
    @types/node>=22optional

For example:

import { writeZodTypes } from "@evolve-framework/json-schema-to-zod";
// codegen.types.ts: one validator and type per event this package publishes.
await writeZodTypes(
["product-created", "product-updated"].map((event) => ({
uri: `https://schemas.evolve.labdigital.nl/events/${event}.schema.json`,
})),
"./src/events/generated.ts",
`${import.meta.dirname}/../schemas/src/messages`,
);

Entry points

@evolve-framework/json-schema-to-zodwriteZodTypes(entries, outputFile, schemaRootDir, schemaUrl?)1 export