Zod

noun

say it: “ZAHD” /zɑd/

A JavaScript library that checks whether data has the right shape and values, and lists every problem when it doesn't.

What is Zod?

Zod is a JavaScript library that checks data against a schema you write. safeParse gives back either the cleaned data or a list of every problem, which an API can send back with a 400.

import * as z from "zod";

const Ticket = z.object({ bike: z.string().min(1), price: z.number().positive() });
const result = Ticket.safeParse({ bike: "", price: -5 });
console.log(result.success);             // false
console.log(result.error.issues.length); // 2

In a sentence

  • “Zod also checks the settings at startup, so a missing PORT stops the app with a clear message.”
  • “The repair shop's API checks every new ticket with Zod before saving it.”

Level: Journeyman · More JavaScript terms at this level

Updated