npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

zod-schema-faker

v2.0.2

Published

Generates mock data from zod schema. Powered by @faker-js/faker and randexp.js

Readme

zod-schema-faker

Generate mock data from zod schemas. Powered by @faker-js/faker and randexp.js.

CI NPM

Features

  • Support zod v3, v4 and mini
  • Support almost all zod types
  • Support for custom zod types
  • Extensive tests

Installation

npm install --save-dev zod-schema-faker

Usage

Setup

v3:

import { install } from 'zod-schema-faker' // alias: 'zod-schema-faker/v3'

install()

v4 or mini:

import { setFaker } from 'zod-schema-faker/v4'
import { faker } from '@faker-js/faker'

setFaker(faker)

Fake Built-in types

import { fake } from 'zod-schema-faker'

const Player = z.object({
  username: z.string(),
  xp: z.number(),
})

const data = fake(Player)
console.log(data) // { username: "billie", xp: 100 }

Fake Custom types

v3:

import { installCustom, fake, getFaker, ZodTypeFaker } from 'zod-schema-faker'

// define a custom zod schema
const pxSchema = z.custom<`${number}px`>(val => {
  return typeof val === 'string' ? /^\d+px$/.test(val) : false
})

// define a custom faker
class ZodPxFaker extends ZodTypeFaker<typeof pxSchema> {
  fake(): `${number}px` {
    return `${getFaker().number.int({ min: 0 })}px`
  }
}

// call installCustom() to register custom faker
installCustom(pxSchema, ZodPxFaker)

// generate fake data based on schema
const data = fake(pxSchema) // '100px'

v4 or mini:

import { custom, fake, Fake, getFaker } from 'zod-schema-faker/v4'

// define a custom zod schema
const pxSchema = z.custom<`${number}px`>(val => {
  return typeof val === 'string' ? /^\d+px$/.test(val) : false
})

// define a custom faker
const fakePxSchema: Fake<typeof pxSchema> = () => {
  return (getFaker().number.int({ min: 1, max: 100 }) + 'px') as `${number}px`
}

// call custom() to register custom faker
custom(pxSchema, fakePxSchema)

// generate fake data based on schema
const data = fake(pxSchema) // '100px'

API

v3

Core APIs

  • function install(): void: Install fakers for built-in types, must be called before using fake.
  • function fake<T extends z.ZodType>(schema: T): z.infer<T>: Generate fake data based on schema.
  • class ZodSchemaFakerError

Random Utility APIs

  • function seed(value?: number): void: Sets the seed to use.
  • function setFaker(faker: Faker): void: Use given faker instance instead of the default one.
  • function getFaker(): Faker: Get the faker instance. Defaults to fakerEN.
  • function randexp(pattern: string | RegExp, flags?: string): string: Create random strings that match a given regular expression.

Customization APIs - see example for details

  • class ZodTypeFaker: Base class for fakers.
  • function installCustom<T extends z.ZodTypeAny>(schema: T, faker: typeof ZodTypeFakerConcrete<T>): void: Install fakers for custom schemas, must be called before using fake.

v4

Core APIs

  • function fake<T extends core.$ZodType>(schema: T): core.infer<T>: Generate fake data based on schema.

Random Utility APIs

  • function seed(value?: number): void: Sets the seed or generates a new one. This method is intended to allow for consistent values in tests, so you might want to use hardcoded values as the seed.
  • function setFaker(faker: Faker): void: Set the faker instance to use.
  • function getFaker(): Faker: Get the faker instance.
  • function randexp(pattern: string | RegExp, flags?: string): string: Create random strings that match a given regular expression.

Customization APIs

  • function custom<T extends core.$ZodType>(schema: T, fake: Fake<T>): void: Generate fake data based on schema.
  • type Fake<T extends core.$ZodType> = (schema: T, context: Context, rootFake: RootFake) => core.infer<T>: Custom fake function.

Unsupported

v3

  • .refine ❌
  • .superRefine ❌

v4

  • .codec 🚧
  • .file 🚧
  • .intersection 🚧
  • .preprocess 🚧
  • .refine ❌
  • .stringbool custom 🚧
  • .stringFormat custom 🚧
  • .superRefine ❌

About

Distributed under the MIT license. See LICENSE for more information.