jsontoschema
All posts
zodschemacomparisontypescript

Generated Schemas vs Zod for Runtime Validation

Zod is the go-to library for runtime validation in TypeScript. You define a schema in code, parse incoming data against it, and get type-safe output. It works well. But it requires you to write the schema first, and for many use cases, that is the hard part.

A generated schema takes the opposite approach. You start with actual JSON, and the tool produces the structural description for you. The question is when each approach makes sense.

What Zod gives you

Zod schemas are executable. They parse data, return typed results, and throw structured errors when validation fails. A simple example:

import { z } from "zod";

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

type User = z.infer<typeof UserSchema>;

const result = UserSchema.safeParse(apiResponse);

This is powerful when you control the contract. You decide what fields are required, add format constraints like .email(), and get compile-time types for free. Zod is the right tool when you are defining what data should look like.

Where generated schemas fit

Generated schemas solve a different problem: describing what data actually looks like. You paste a real API response, and the tool outputs the structure. No manual transcription, no guessing whether metadata is optional or which fields are nullable.

{
  name: string,
  email: string,
  age?: number
}

This compact output is useful in several places where Zod is overkill:

  • LLM prompts. You need to tell a model the shape of an API response. A compact schema costs ~80% fewer tokens than the raw JSON and conveys the same structural information. Zod syntax is not meaningful to a language model.
  • Documentation. A generated schema reads like a type definition. Zod schemas read like code. For API docs or READMEs, the compact format communicates structure faster.
  • Exploration. When you hit an unfamiliar endpoint and want to understand the shape before writing any validation code, generating a schema takes seconds.

They complement each other

The practical workflow often uses both. You receive a JSON response from a new API. You generate a schema to understand the structure. Then, if you need runtime validation, you write a Zod schema informed by what you learned.

The generated schema answers "what does this data look like?" Zod answers "what should this data look like, and what happens when it does not?"

Trying to use Zod for exploration means writing schemas by hand before you understand the data. Trying to use generated schemas for validation means losing format constraints, custom error messages, and parse-time transforms. Each tool has a clear boundary.

When to skip Zod entirely

Not every project needs runtime validation. If you are building an internal tool that consumes a single API, and that API is stable, TypeScript interfaces generated from a sample response may be sufficient. You get autocomplete and compile-time checks without the overhead of a validation library.

Generated TypeScript interfaces give you this directly. Paste the response, copy the interfaces, and move on. Add Zod later if the data source proves unreliable.

The short version

Use generated schemas to understand and communicate structure. Use Zod to enforce it. Start with generation, graduate to validation when the use case demands it.

Try pasting a JSON response into the converter to see the difference firsthand.