Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

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

License

NotificationsYou must be signed in to change notification settings

soc221b/zod-schema-faker

Repository files navigation

Generate mock data fromzod schemas. Powered by@faker-js/faker andrandexp.js.

CINPM

Features

  • Support almost all zod types
  • Support for custom zod types
  • Extensive tests

Installation

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

Usage

Built-in zod types:

import{z}from'zod'import{install,fake}from'zod-schema-faker'// define a zod schemaconstschema=z.number()// call install() to register fakersinstall()// generate fake data based on schemaconstdata=fake(schema)

Custom zod types:

import{z}from'zod'import{installCustom,fake,getFaker,ZodTypeFaker}from'zod-schema-faker'// define a custom zod schemaconstpxSchema=z.custom<`${number}px`>(val=>{returntypeofval==='string' ?/^\d+px$/.test(val) :false})// define a custom fakerclassZodPxFakerextendsZodTypeFaker<typeofpxSchema>{fake(): `${number}px`{return`${getFaker().number.int({min:0})}px`}}// call installCustom() to register custom fakersinstallCustom(pxSchema,ZodPxFaker)// generate fake data based on schemaconstdata=fake(pxSchema)

API

Core APIs

  • 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.
  • function seed(value?: number): void: Sets the seed to use.
  • class ZodSchemaFakerError

Random Utility APIs

  • 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 regularexpression.

Customization APIs - seeexample for details

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

Supported Zod Types

  • 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: seeexample for details.
  • ✅ z.date
  • ✅ z.discriminatedUnion
  • ✅ z.enum
  • ✅ z.instanceof: seeexample 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

Comparison

@anatine/zod-mock

https://github.com/anatine/zod-plugins/tree/main/packages/zod-mock

  • Excels at generating realistic data forz.string. For instance,z.object({ image: z.string() }) will produce a URLstring.
  • Lacks support for some basic zod types such asz.any,z.default,z.tuple, etc.

zod-fixture

https://github.com/timdeschryver/zod-fixture

  • Provides support for custom zod types.
  • Occasionally generates invalid mocked data. For example, calling with the function generated fromz.function(z.tuple([]), z.boolean()) did not return a boolean value.

Inactive Libraries

About

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

Footnotes

  1. Not applicable, ignored

  2. Not compatible with other validations. For example,z.length(5) is ignored inz.base64().length(5).


[8]ページ先頭

©2009-2025 Movatter.jp