Generate mock data from zod schemas. Powered by @faker-js/faker and randexp.js.
Features
- Support almost all zod types
- Support for custom zod types
- Extensive tests
npm install --save-dev zod-schema-faker
Built-in zod types:
import { z } from 'zod'
import { install, fake } from 'zod-schema-faker'
// define a zod schema
const schema = z.number()
// call install() to register fakers
install()
// generate fake data based on schema
const data = fake(schema)
Custom zod types:
import { z } from 'zod'
import { installCustom, fake, runFake, 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 `${runFake(faker => faker.number.int({ min: 0 }))}px`
}
}
// call installCustom() to register custom fakers
installCustom(pxSchema, ZodPxFaker)
// generate fake data based on schema
const data = fake(pxSchema)
Core APIs
function install()
: register fakers, must be called before usingfake()
function fake(schema)
: generate fake data based on schemafunction seed(value)
: sets the seed to useclass ZodSchemaFakerError
: thrown when an error occurs, such as when a corresponding faker is not registered
Random Utility APIs
function installFaker(faker)
: install a custom faker, must be called before usingrunFake()
. defaults tofakerEN
from@faker-js/faker
function runFake(runner)
function randexp(pattern, flags)
Customization APIs - see example for details
class ZodTypeFaker
: base class for custom fakersfunction installCustom(schema, faker)
: register a custom faker
- methods
- ✅ .and
- ✅ .array
- ✅ .brand
- ✅ .catch
- ✅ .default
- ✅ .nullable
- ✅ .nullish
- ✅ .optional
- ✅ .or
- ✅ .pipe
- ✅ .promise
- ✅ .readonly
- ❌ .refine
- ❌ .superRefine
- ✅ .transform
- ✅ z.any
- ✅ z.array
- ✅ z.bigint
- ✅ z.boolean
- ✅ z.custom: see example for details.
- ✅ z.date
- ✅ z.discriminatedUnion
- ✅ z.enum
- ✅ z.instanceof: see example for details.
- ✅ z.intersection
- ✅ z.function
- ✅ z.lazy
- ✅ z.literal
- ✅ z.map
- ✅ z.nan
- ✅ z.nativeEnum
- ✅ z.never: always throws an error
- ✅ z.null
- ✅ z.number
- ✅ z.object
- ✅ z.preprocess1
- ✅ z.promise
- ✅ z.record
- ✅ z.set
- ✅ z.string2
- ✅ z.symbol
- ✅ z.tuple
- ✅ z.undefined
- ✅ z.union
- ✅ z.unknown
- ✅ z.void
https://github.com/anatine/zod-plugins/tree/main/packages/zod-mock
- Excels at generating realistic data for
z.string
. For instance,z.object({ image: z.string() })
will produce a URL string. - Lacks support for some basic zod types such as
z.any
,z.default
,z.tuple
, etc.
https://github.com/timdeschryver/zod-fixture
- Provides support for custom zod types.
- Occasionally generates invalid mocked data. For example, calling with the function generated from
z.function(z.tuple([]), z.boolean())
did not return a boolean value.
- https://github.com/dipasqualew/zod-mocking
- https://github.com/LorisSigrist/zocker
- https://github.com/ItMaga/zodock
Distributed under the MIT license. See LICENSE for more information.