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
npm install --save-dev zod-schema-fakerv3:
import { install, fake } from 'zod-schema-faker' // alias: 'zod-schema-faker/v3'
install()v4 or mini:
import { setFaker, fake } from 'zod-schema-faker/v4'
import { faker } from '@faker-js/faker'
setFaker(faker)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:
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'function install(): void: Install fakers for built-in types, must be called before usingfake.function fake<T extends z.ZodType>(schema: T): z.infer<T>: Generate fake data based on schema.class ZodSchemaFakerError
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 tofakerEN.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 usingfake.
function fake<T extends core.$ZodType>(schema: T): core.infer<T>: Generate fake data based on schema.
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.
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.
- .refine β
- .superRefine β
- .codec π§
- .file π§
- .intersection π§
- .preprocess π§
- .refine β
- .stringbool custom π§
- .stringFormat custom π§
- .superRefine β
Distributed under the MIT license. See LICENSE for more information.