Zod or a JSON Schema — which one should you keep?
Both check data. One lives in your TypeScript, one lives in a file every language can read. The choice depends on how many languages you have.
You want the shape of your data written down once, and checked when the program runs. Two good answers exist, and people argue about them for the wrong reasons.
The short answer
- One language, TypeScript: keep Zod.
- More than one language, or an API other teams call: keep a JSON Schema.
Everything else follows from that.
What each one is
A Zod schema is TypeScript code:
import * as z from 'zod'
export const UserSchema = z.object({
id: z.number(),
name: z.string(),
email: z.string().email(),
})
export type User = z.infer<typeof UserSchema>
A JSON Schema is a JSON document:
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"properties": {
"id": { "type": "integer" },
"name": { "type": "string" },
"email": { "type": "string", "format": "email" }
},
"required": ["id", "name", "email"]
}
Open this example in the tool →
Where Zod wins
The type comes free. z.infer gives you the TypeScript type from the
schema, so the check and the type cannot disagree. With a JSON Schema you
generate the type in a build step, and somebody will forget to run it.
It is code, so it composes. UserSchema.extend({...}), .partial(),
.omit(), a union of two schemas, a value that depends on another field.
Writing that in JSON Schema is possible and painful.
The messages are good. A failed parse names the path and says what it wanted.
Transforms. z.coerce.date(), .trim(), .transform(). The schema both
checks and converts, which is usually what you want at an edge.
Where a JSON Schema wins
Every language can read it. Python, Go, Java, Rust and C# all have checkers. Your Zod schema is TypeScript, and nothing else can use it.
It is the standard for an API. OpenAPI holds JSON Schema. If you publish an API, the schema is part of the contract and your consumers already know how to read it.
It survives. A JSON file in a repository outlives the library that read it. Zod has already had breaking changes between major versions.
It can be the one source for every language. Keep the schema, generate the types for TypeScript, Python, Go and the rest in your build. That is the pattern this whole tool is built around.
You can have both
This is the part people miss. A JSON Schema can be the source, and Zod can be generated from it. Several libraries do that conversion. So:
- Keep
user.schema.jsonin the repository. It is the contract. - Generate the Go struct, the Python model and the Zod schema from it in CI.
- Your TypeScript gets the ergonomics of Zod, and your Go service gets the same rules, from the same file.
The cost is a build step and a rule that nobody edits the generated files. The gain is that two services cannot disagree about what a user is.
When neither is worth it
A script that runs once. An internal tool with one user. A prototype. Checking data costs lines, and lines cost attention. Spend them where a wrong value would cost you something.
How to start
You probably have neither, and some JSON. So:
- Paste a real answer into the tool and pick JSON Schema. Read what it wrote. It marks everything required, because everything was in your sample, so fix that first.
- Add the facts a sample cannot hold: which fields are truly required, what a number may be, what a string must look like.
- Commit it. Now pick Zod in the tool and compare — or generate the Zod from the schema if your build can.
Try it
Paste this and look at both outputs:
{ "id": 1, "name": "Ada", "email": "ada@example.com", "role": "admin" }
Open this example in the tool →
Note what neither one knows: that role has only three legal values. That is
the kind of fact you add by hand, once, in whichever file you decided to keep.
Try it on your own JSON. The generator runs in your browser. Nothing is uploaded, and nothing is stored on a server.
Open JSON to CodeRead next
An enum, or a string?
A generator sees "admin" and writes string. Sometimes that is right and sometimes it throws away the most useful fact in your data.
A type is not a check
TypeScript, Flow and Elm types vanish when the program runs. If your data comes from outside, you need something that is still there at run time.