commonbaseapp/zodex(opens in a new tab)
Language: TypeScript
The missing zod (de/)serialization library
- stars
- 154
- watchers
- 4
- forks
- 8
- open issues
- 3
- Last updated
- 09/17/2025
Language: TypeScript
The missing zod (de/)serialization library
Type-safe (de)serialization library for zod (v4 format). It both serializes and simplifies types into a JSON format, in the following ways:
{ "type": "string", "defaultValue": "hi" }{ "type": "number", "min": 23, "max": 42 }pnpm add zodex # or yarn add zodex # or npm install zodex
import { z } from "zod"; import { zerialize } from "zodex"; const someZodType = z.discriminatedUnion("id", [ z.object({ id: z.literal("a"), count: z.number().optional() }), z.object({ id: z.literal("b") }), ]); const shape = zerialize(someZodType);
Now typeof shape will be
type Shape = { type: "discriminatedUnion"; discriminator: "id"; options: [ { type: "object"; properties: { id: { type: "literal"; values: ["a"] }; count: { type: "number"; isOptional: true }; }; }, { type: "object"; properties: { id: { type: "literal"; values: ["b"] } } }, ]; };
which is exactly equal to its runtime value (shown in YAML for brevity, you probably shouldn't use YAML):
type: "discriminatedUnion" discriminator: "id" options: - type: "object" properties: id: type: "literal" values: - "a" count: type: "number" isOptional: true - type: "object" properties: id: type: "literal" values: - "b"
Both zerialize and dezerialize accept an options object with the
same errors, checks, and transforms properties.
Since Zod does not allow the specification of the names of errors, checks and transforms (and preprocesses), we allow you to supply as options maps of names to errors/checks/transforms so that these can be part of serialization and deserialization. If none of these options are supplied, the errors/checks/transforms will be omitted.
Properties:
errors - Map of name to .someType({error: fn}) functionschecks - Map of name to .checks() functionstransforms - Map of name to .transform() (and .preprocess) functionsJSON references are used to represent local references. If you wish to use
JSON references for remote (non-cyclic) references, you may do so, but you
will need to use a library like
json-refs (with resolveRefs)
to first resolve such references and then supply the object to dezerialize.
Zodex will serialize local references, including handling recursive ones. As
with JSON Schema, the $defs property may be a reasonable top-level property
to use as storage for local references, but it receives no special treatment
by this library (any property could be targeted by one's references).
Note that if you wish to use additional properties with an item containing a
reference, e.g., isOptional, you will first need to wrap the JSON reference
within a single-item union such as in the following:
{
"type": "union",
"options": [
{
"$ref": "#/properties/id"
}
]
}Note that due to technical limitations with Zod, we are unable to allow a
JSON reference in place of an object properties object. You can either
resolve this first with another library (if it is a non-cyclic reference),
or target the whole object or individual properties.
brand is not supportable and omittedlazy type is unwrapped; pipe is unwrapped when no transforms are suppliedcatch with a function can have its then-value serialized but it
cannot then be deserialized back into using the original functionrefine(), custom() and instanceof methods (and they will be
ignored), but these are really just implementations of check()
which is supported