JSON to Zod with Custom Validation

Generate Zod schemas from JSON with customizable validation rules like .min(), .max(), .email(), .url(), and .regex().

What is JSON to Zod with Custom Validation?

Zod is a TypeScript-first schema declaration and validation library that lets you define schemas validating data at runtime while inferring TypeScript types at compile time. This generator goes beyond basic type inference by allowing you to customize validation rules for each field type. It automatically detects common patterns — email addresses, URLs, datetime strings, integers, and positive numbers — and applies the appropriate Zod validators. The advanced options panel gives you control over string length limits (.min(), .max()), number ranges, and array sizes, so the generated schema matches your business requirements rather than just the raw data shape. The output includes both the schema definition and an inferred TypeScript type.

How to Use

  1. Paste your JSON data into the input area on the left
  2. Set the schema name in the input field — this becomes the exported variable name
  3. Click "Show Validation Options" to reveal the advanced settings panel
  4. Configure string length limits, number ranges, array sizes, and toggle auto-detection for emails, URLs, and datetimes
  5. Click "Generate" to produce the Zod schema with your custom validation rules
  6. Copy the output into your TypeScript project and use schema.parse() or schema.safeParse() for validation

Why Use This Tool?

Automatic type inference from JSON values with smart detection of emails, URLs, and datetime formats
Customizable validation rules for strings (.min(), .max()), numbers (.int(), .positive(), ranges), and arrays (.min(), .max())
Auto-detection adds .email(), .url(), and .datetime() validators for strings that match those patterns
Auto-detection for integers (.int()) and positive numbers (.positive()) produces stricter number validation
Generated TypeScript type inference with z.infer gives you compile-time types from the same schema
Nested objects and arrays are fully supported with recursive type inference

Tips & Best Practices

  • Provide JSON with realistic values — email and URL detection depend on recognizable formats in the actual data
  • Enable auto-detection for emails, URLs, and datetimes to get stricter validation without manual configuration
  • Set appropriate min/max values for your use case — the defaults are intentionally permissive
  • The generated schema includes a TypeScript type alias via z.infer for immediate use in your codebase
  • Use schema.safeParse() instead of schema.parse() to get a result object without throwing on validation failure

Frequently Asked Questions

How are JSON types mapped to Zod types?

JSON strings map to z.string(), numbers to z.number() or z.number().int() for integers, booleans to z.boolean(), null to z.null(), arrays to z.array(), and objects to z.object({}). Auto-detection adds .email(), .url(), and .datetime() for matching string patterns, and .int() and .positive() for qualifying numbers.

When should I NOT use this generator?

Skip this tool if you need Zod features that cannot be inferred from a single JSON sample, such as discriminated unions (z.discriminatedUnion), recursive schemas (z.lazy), transforms (z.transform), or refinements (z.refine). Also, if you need conditional validation (e.g., field B is required when field A equals a certain value), those must be added manually.

What is Zod and why should I use it?

Zod is a TypeScript-first schema declaration and validation library. It lets you define schemas that validate data at runtime and infer TypeScript types at compile time from the same definition. It is widely used for API request validation, form validation, environment variable parsing, and configuration validation.

How do I use the generated schema?

Import the schema in your TypeScript file, then use schema.parse(data) to validate and return the parsed data (throws on failure), or schema.safeParse(data) to get a result object with success and error properties. The inferred type is exported as SchemaNameType using z.infer.

Can I add custom validation rules?

Yes. Click "Show Validation Options" to configure string length limits (.min(), .max()), number ranges, array sizes, and toggle auto-detection for emails, URLs, datetimes, integers, and positive numbers. For more advanced rules like .regex() or .refine(), add them manually to the generated schema.

Is my data sent to any external server?

No. All processing happens entirely inside your browser. Your JSON data never leaves your device, and no network requests are made during the conversion.

Real-world Examples

Validating a user registration form with email detection

Generate a Zod schema that automatically validates email format and applies string length constraints.

Input
{
  "name": "Alice",
  "email": "alice@example.com",
  "age": 25,
  "website": "https://alice.dev"
}
Output
import { z } from 'zod';

export const UserSchema = z.object({
  name: z.string().min(1).max(100),
  email: z.string().email(),
  age: z.number().int().positive().min(0).max(100),
  website: z.string().url(),
});

export type UserSchemaType = z.infer<typeof UserSchema>;

API response schema with nested objects and datetime detection

A JSON API response with nested objects and ISO datetime strings generates a complete Zod schema with proper nesting.

Input
{
  "id": 1,
  "title": "My Post",
  "createdAt": "2024-01-15T10:30:00Z",
  "author": {
    "name": "Bob",
    "email": "bob@example.com"
  }
}
Output
import { z } from 'zod';

export const AuthorSchema = z.object({
  name: z.string().min(1).max(100),
  email: z.string().email(),
});

export const PostSchema = z.object({
  id: z.number().int().positive().min(0).max(100),
  title: z.string().min(1).max(100),
  createdAt: z.string().datetime(),
  author: AuthorSchema,
});

export type PostSchemaType = z.infer<typeof PostSchema>;

Related Tools