Generates mock data from zod schema. Powered by @faker-js/faker and randexp.js
npm install zod-schema-faker> Generate mock data from zod schemas. Powered by
> @faker-js/faker and randexp.js.


Features
- Support zod v3, v4 and mini
- Support almost all zod types
- Support for custom zod types
- Extensive tests
``sh`
npm install --save-dev zod-schema-faker
v3:
`ts
import { install } from 'zod-schema-faker' // alias: 'zod-schema-faker/v3'
install()
`
v4 or mini:
`ts
import { setFaker } from 'zod-schema-faker/v4'
import { faker } from '@faker-js/faker'
setFaker(faker)
`
`ts
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 }
`
v3:
`ts
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
fake(): ${number}px {${getFaker().number.int({ min: 0 })}px
return
}
}
// call installCustom() to register custom faker
installCustom(pxSchema, ZodPxFaker)
// generate fake data based on schema
const data = fake(pxSchema) // '100px'
`
v4 or mini:
`ts
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
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'
`
#### Core APIs
- function install(): void: Install fakers for built-in types, must be called before using fake.function fake
- : 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
- : Installfake
fakers for custom schemas, must be called before using .
#### Core APIs
- function fake: 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 forfunction setFaker(faker: Faker): void
consistent values in tests, so you might want to use hardcoded values as the seed.
- : 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: Generate fake data based on schema.type Fake
-
function.
- .refine ❌
- .superRefine ❌
- .codec 🚧
- .intersection 🚧
- .preprocess 🚧
- .refine ❌
- .stringbool custom 🚧
- .stringFormat custom 🚧
- .superRefine ❌
Distributed under the MIT license. See LICENSE for more information.