Zod

Zod logo

Overview

Zod is a TypeScript library for validating JavaScript values.

The rationale for the name "Zod" is not given in its documentation, but some definitions include:

The following sections provide an overview of specifying validation constraints using Zod. For more detail, see the Zod home page.

Primitive Types

Primitive types are specified with the syntax z.{type}(). They include:

These types are all functions that are typically called with no arguments. Some of them take an optional configuration argument. Rather that calling these functions repeatedly, they can be called once to store the return value in a variable and the variable can be used multiple times.

Strings

Many validation methods can be applied to the string type. Highlights include the following:

For example, the type z.string().min(3).max(10).endsWith("X") matches string values with a length of at least 3, not more than 10, and ending with "X".

Numbers

Many validation methods can be applied to the number type. Highlights include the following:

All validation methods take an optional final argument that specifies the error message to display when the validation fails. For example, z.number().max(10, { message: "cannot exceed 10" })

Enumerations

The enum type specifies a set of allowed string values. For example, z.enum(["red", "green", "blue"])

To validate against the values in a TypeScript enum, use the nativeEnum type. For example:

enum Color { red, green, blue }
...
z.nativeEnum(Color)

Optional Values

All types specify a required value unless the optional method is used. For example: z.optional(z.string()) or z.string().optional().

Objects

Object types are specified with the object method. For example:

const DogSchema = z.object({
name: z.string(),
breed: z.string(),
age: z.number().optional()
});

Arrays

Array types are specified with the array function. For example, the following both specify an array of integers:

z.array(z.number().int());
z.number().int().array();

Infering TypeScript Types

To generate a TypeScript type from a Zod schema object, use z.infer. For example:

type Dog = z.infer<typeof DogSchema>;