Zocker: Zod Schema Test Data Generator

3.0.0 · active · verified Sun Apr 19

Zocker is a library designed to generate realistic, type-safe test data directly from your Zod schemas. It provides a fluent API for defining how mock data should be created, allowing for customization of various Zod types and the ability to override generation rules globally or for specific schema paths. The current stable version is 3.0.0, which solidified its intuitive fluent API after a significant breaking change in v1.0.0. While there isn't a strict release cadence, minor and patch versions are released as new Zod features are supported or bug fixes are implemented. Zocker differentiates itself by deeply integrating with Zod, leveraging the schema definition itself to ensure generated data adheres to the schema's constraints and types, making it ideal for robust testing and prototyping.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to define a Zod schema and then use `zocker` to generate both single and multiple mock data objects that conform to that schema.

import { z } from 'zod';
import { zocker } from 'zocker';

// Define a Zod schema for a User
const UserSchema = z.object({
  id: z.string().uuid(),
  name: z.string().min(3).max(50),
  email: z.string().email(),
  age: z.number().int().min(18).max(99),
  isActive: z.boolean(),
  roles: z.array(z.enum(['admin', 'editor', 'viewer'])).min(1),
  createdAt: z.date().default(() => new Date()),
});

// Create a zocker instance for the User schema
const generateUser = zocker(UserSchema);

// Generate a single user object
const user = generateUser();
console.log('Generated User:', user);

// Generate multiple user objects (e.g., 5 users)
const users = zocker(UserSchema, { seed: 123 }).generateMany(5);
console.log('Generated Users (many):', users);

view raw JSON →