zodproto-ts
v1.0.2
Published
Readme
zodproto-ts
zodproto-ts is a protoc plugin that generates TypeScript + Zod schemas from protobuf definitions.
The generator targets proto3 and emits:
- Zod schema per message and enum
- TypeScript type aliases inferred from Zod
- Binary serializer helpers with encode/decode and optional decode-time validation
Note: Additional validations (e.g. min, max, pattern matching, ...) are not supported yet.
Disclaimer: This project is in early development, bugs are to be expected.
Generated API
For each protobuf message <M>, the generator emits:
<M>Schema- type
<M> <M>Serializer
Example usage:
import { SampleSchema, SampleSerializer, type Sample } from "./gen/sample.js";
const input: Sample = SampleSchema.parse({
id: 1n,
value: 42,
is_active: true,
deltas: [1, -1],
raw_payload: new Uint8Array(),
counters: {},
labels: {},
history: [],
source: { case: undefined },
});
const bytes = SampleSerializer.encode(input).finish();
const decoded = SampleSerializer.decode(bytes);
const validatedDecoded = SampleSerializer.decode(bytes, { validate: true });Usage with protoc
Windows (PowerShell), minimal setup:
npm i -D zodproto-ts
protoc -I .\protos --plugin=protoc-gen-zodproto-ts=.\node_modules\.bin\protoc-gen-zodproto-ts.cmd --zodproto-ts_out=.\gen --zodproto-ts_opt=target=ts .\protos\*.protoUnix (bash), minimal setup:
npm i -D zodproto-ts
protoc -I ./protos --plugin=protoc-gen-zodproto-ts=./node_modules/.bin/protoc-gen-zodproto-ts --zodproto-ts_out=./gen --zodproto-ts_opt=target=ts ./protos/*.protoDevelopment
Build:
pnpm run buildEnd-to-end test (build plugin, generate test outputs, compile tests, run node:test):
pnpm testLicense
MIT
