TryJS
Zod 4.6.7 · TypeScriptZodnpmValidationData

Zod 4 Validation Playground — TypeScript safeParse Example

Try a real package: validate unknown data and inspect readable errors.

Loading playground…

The live editor loads here with JavaScript enabled.

Run in Playground ↗

The original source and explanation are available below.

Changes here stay separate from your main playground. Use Open in TryJS inside the editor to keep working with your changes.

Useful? Help keep TryJS free and independent.

Support TryJS

What to expect

The initial input fails email and minimum-age validation. Valid data produces a typed user object.

About this example

TypeScript types disappear at runtime. Data received from an API, a form or AI-generated code still needs validation. This example imports a specific Zod release and uses safeParse so expected validation errors are values rather than thrown exceptions.

How It Works

Describe the runtime schema

The schema checks an email string and an age of at least 18. z.infer derives a TypeScript type from those same rules.

Inspect both outcomes

safeParse returns a discriminated union. Read result.data after success, or result.error.issues after failure. Changing the input reruns the code.

Read the original source

This is the starting code. Your edits stay in the live editor above.

TypeScript

import { z } from "zod@4.6.7";

const User = z.object({
  email: z.string().email(),
  age: z.number().int().min(18),
});
type User = z.infer<typeof User>;

const input: unknown = { email: "not-an-email", age: 16 };
const result = User.safeParse(input);

if (result.success) {
  const user: User = result.data;
  console.log("Valid user:", user);
} else {
  console.log("Validation issues:", result.error.issues);
}