Check out the accompanying video NextJS Form Validation Example with Zod on Coding Money youtube channel.
Here are some common types and how you can define schemas for them:
const numberSchema = z.number().min(1).max(100); // A number between 1 and 100 const booleanSchema = z.boolean(); // A boolean value (true or false) const dateSchema = z.date().min(new Date('2023-01-01')); // A date after January 1st, 2023 const arraySchema = z.array(z.string()); // An array of strings const numberArraySchema = z.array(z.number()).min(3); // An array of numbers with at least 3 items const roleEnum = z.enum(["Admin", "User", "Guest"]); // Enum for specific roles const objectSchema = z.object({ name: z.string(), age: z.number().min(18), // Nested validation within an object }); const nullableSchema = z.string().nullable(); // Can be a string or null const optionalSchema = z.string().optional(); // Can be a string or undefined You can add custom validations using .refine():
const passwordSchema = z.string().refine((val) => val.length >= 6, { message: "Password must be at least 6 characters long", }); const stringOrNumber = z.union([z.string(), z.number()]); // Can be a string or a number Zod also allows you to transform values:
const transformSchema = z.string().transform((val) => val.toUpperCase()); // Converts string to uppercase You can chain multiple validators:
const advancedSchema = z.string().min(5).max(10).regex(/^[a-zA-Z]+$/); // String between 5 and 10 characters containing only letters References: You can find more types and validations in the Zod documentation.