consolelog.tools

Zod Schema Generator

Paste a JSON sample and get a smart Zod schema — not a naive one. It merges every element of an array to infer which fields are truly optional, which are nullable, and which are enums (from repeated string values), detects string formats (email, url, uuid, datetime, date, IPv4), and emits the matching z.infer TypeScript type. A live validator lets you paste a second payload and see exactly which fields would fail, and the whole setup is captured in a shareable URL.

About this ToolHow it works, benefits & use cases

Paste a JSON sample and get a smart Zod schema, not a naive one-to-one translation. The generator merges every element of an array to work out which fields are genuinely optional (missing from some records), which are nullable (seen as null somewhere), and which are enums (a string field that only ever takes a small, repeating set of values). It detects string formats — email, url, uuid, datetime, ISO date and IPv4 — and emits the matching z.infer TypeScript type alongside the schema. A built-in validator lets you paste a second payload and instantly see which field paths would fail, so the tool both generates and checks. Toggle strict objects, primitive coercion, format/enum detection, and switch the output between the Zod schema and a plain inferred TypeScript type. The whole configuration lives in a shareable URL.

How to Use

  1. 1Paste a JSON object — or better, an array of records — into the input (or load an example).
  2. 2Name the schema and toggle inference options: format detection, enum detection, strict objects, coercion.
  3. 3Read the generated Zod schema and the inferred z.infer type; switch the output toggle to see a plain TS type.
  4. 4Check the confidence card (how many fields, optionals, nullables, enums and formats were inferred).
  5. 5Paste another JSON value into "Validate a payload" to see exactly which fields pass or fail, then copy or share.

Key Benefits

  • Array-sample merging infers truly optional and nullable fields instead of marking everything required
  • Enum detection turns a repeated small string set into z.enum([...])
  • String-format detection: email, url, uuid, datetime, date and IPv4
  • Emits the matching z.infer TypeScript type, or a plain inferred type via the output toggle
  • A live validator reports the exact failing field paths for any payload
  • Strict-objects, coercion, and format/enum toggles for full control
  • Shareable URL captures the JSON, options and output mode

Common Use Cases

  • Turning a sample API response into a Zod schema for request/response validation
  • Bootstrapping form or env validation from an example payload
  • Inferring which fields are optional or nullable from a list of real records
  • Deriving a z.enum from a status/role field that only takes a few values
  • Confirming a draft schema accepts the payloads you expect and rejects the ones you do not

Zod schema + inferred type

import { z } from 'zod';

export const schema = z.array(z.object({
  sku: z.string(),
  price: z.number(),
  status: z.enum(['active', 'archived']),
  tags: z.array(z.string()).optional(),
  featured: z.boolean().optional(),
}));

export type Schema = z.infer<typeof schema>;
Schema validates the source sample
5fields2optional0nullable1enums0formats

Validate a payload

JSON sample

Tip: paste an array of records — the more samples, the smarter the optional / nullable / enum inference.

Inference & output

email, url, uuid, datetime…

repeated small string sets

reject unknown keys

z.coerce.*

Why an array sample is worth more

From a single object the tool can only guess every field is required and non-null. Paste an array and it merges all elements: a key missing from any element becomes .optional(), a value seen as null anywhere becomes .nullable(), and a string field with a small set of repeated values becomes a z.enum([...]).

It validates, not just generates

The “Validate a payload” box runs the inferred schema against any JSON you paste and lists the exact failing field paths — so you can confirm the schema accepts what you expect and rejects what you don’t.

Was this tool helpful?

Share Your Experience

Help others discover this tool!

Related tools