A serialisation and deserialisation libarary for Zod.
This is useful when there is a need to keep a list of historic versions of zod schema.
npm install zodser
yarn add zodser
To serialise a zod schema to a string for saving
import { serialise } from "zodser";
const schemaString = serialise(zodSchema);
To deserialise a previously serialised zod schema string
import { deserialise } from "zodser";
const zodSchema = deserialise(schemaString);
Currently, all built-in Zod types except ZodEffect are supported with below known limitations
Only constant catch value is supported
// constant value is supported
z.string().catch("fallback");
// catch function that returns a constant value is supported
z.string().catch(() => "fallback");
// this is also okay
const s = "fallback";
z.string().catch(() => s);
// this is not supported, because a different Date will be returned for each call; in this example, timestamp of when serialisation runs will be captured and is used as the catch value after deserialisation
z.date().catch(() => new Date());