Serialize and deserialize zod schemas to and from a compact binary format.
Bundles the pbf package for binary data manipulation.
Requires zod v3 or later.
npm i zod-pbf-binary-serializer
- z.string
- z.number
- z.boolean
- z.date
- z.array
- z.object
- z.instanceof(Uint8Array)
- z.discriminatedUnion
- z.literal(string | number | boolean)
- z.enum
- z.optional
- z.nullable
import { fromSchema } from 'zod-pbf-binary-serializer';
const schema = z.object({
a: z.string(),
b: z.number(),
c: z.boolean(),
d: z.array(z.string()),
});
const serializer = fromSchema(schema);
const data = {
a: 'apple',
b: 123,
c: true,
d: ['hello', 'world'],
};
const buffer = serializer.encode(data);
console.log(buffer);
/**
* Uint8Array(28) [
* 5, 97, 112, 112, 108, 101,
* 0, 0, 0, 0, 0, 192, 94, 64,
* 1,
* 2, 5, 104, 101, 108, 108, 111, 5, 119, 111, 114, 108, 100
* ]
*/
const decoded = serializer.decode(buffer);
console.log(decoded);
// { a: 'apple', b: 123, c: true, d: ['hello', 'world'] }
import { fromSchema, fromBlocks } from 'zod-pbf-binary-serializer';
const schema = z.object({
a: z.string(),
b: z.number(),
c: z.boolean(),
d: z.array(z.string()),
});
const serializer = fromSchema(schema);
console.log(serializer.blocks);
/**
* [
* {
* block: 'primitive',
* type: 'string',
* path: ['a'],
* },
* {
* block: 'primitive',
* type: 'float',
* path: ['b'],
* },
* {
* block: 'primitive',
* type: 'boolean',
* path: ['c'],
* },
* {
* block: 'array',
* type: 'string',
* path: ['d'],
* },
* ]
*/
const reconstructedSerializer = fromBlocks(serializer.blocks);