Zod Info
These are useful zod snippets
Optional vs Nullable vs Nullish
// zod schema
z.object({
// valid if string or:
optional: z.string().optional(), // field not provided, or explicitly `undefined`
nullable: z.string().nullable(), // field explicitly `null`
nullish: z.string().nullish(), // field not provided, explicitly `null`, or explicitly `undefined`
});
// type
{
optional?: string | undefined;
nullable: string | null;
nullish?: string | null | undefined;
}
Async zod refinements
const userId = z.string().refine(async (id) => {
// verify that ID exists in database
return true;
});
⚠️ If you use async refinements, you must use the
.parseAsync
method to parse data! Otherwise Zod will throw an error.
.transform
To transform data after parsing, use the transform
method.
const stringToNumber = z.string().transform((val) => val.length);
stringToNumber.parse("string"); // => 6
Email fails parsing on blank string
''
is not a valid email address, so we need to .union
with a literal ''
const emailSchema = z.union( [
z.literal( '' ),
z.string().email(),
] )
console.log( emailSchema.safeParse( '' ).success ) // true
console.log( emailSchema.safeParse( '[email protected]' ).success ) // true
console.log( emailSchema.safeParse( 'foo' ).success ) // false