Détail du package

zod-schema-faker

soc221b6.4kMIT1.5.4

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

zod, faker-js, fake, stub

readme

zod-schema-faker

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

CI NPM

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 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, 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 fakers
installCustom(pxSchema, ZodPxFaker)

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

API

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

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: 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.preprocess[^2]
  • ✅ z.promise
  • ✅ z.record
  • ✅ z.set
  • ✅ z.string[^1]
  • ✅ z.symbol
  • ✅ z.tuple
  • ✅ z.undefined
  • ✅ z.union
  • ✅ z.unknown
  • ✅ z.void

[^1]: Not compatible with other validations. For example, z.length(5) is ignored in z.base64().length(5).

[^2]: Not applicable, ignored

Comparison

@anatine/zod-mock

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.

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 from z.function(z.tuple([]), z.boolean()) did not return a boolean value.

Inactive Libraries

About

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

changelog

Changelog

All notable changes to this project will be documented in this file. See standard-version for commit guidelines.

1.5.4 (2025-04-16)

Bug Fixes

  • deps: update dependency zod to v3.24.3 (#429) (9c121a5)

1.5.3 (2025-04-13)

Bug Fixes

  • deps: update dependency @faker-js/faker to v9.7.0 (#426) (f2ee323)

1.5.2 (2025-03-08)

Bug Fixes

  • deps: update dependency @faker-js/faker to v9.6.0 (#406) (733f4eb)

1.5.1 (2025-02-28)

Bug Fixes

  • deps: update dependency @faker-js/faker to v9.5.1 (#394) (b0f6666)

1.5.0 (2025-02-28)

Features

  • add jsdocs (5b4f0db)
  • expose getFaker and deprecate runFake (fee613f)
  • rename installFaker to setFaker and deprecate installFaker (4804968)

1.4.1 (2025-02-23)

Bug Fixes

1.4.0 (2025-02-23)

Features

  • support different locales and custom faker instances (#382) (196e990)

1.3.6 (2025-02-12)

Bug Fixes

  • deps: update dependency @faker-js/faker to v9.5.0 (#366) (f6eacb8)

1.3.5 (2025-02-12)

Bug Fixes

  • deps: update dependency zod to v3.24.2 (#364) (1a6534d)

1.3.4 (2025-02-09)

Bug Fixes

  • intersection: it should throw error when intersecting unrelated array schema (#361) (f1d5020)

1.3.3 (2025-02-09)

Bug Fixes

  • intersection: generate valid data for intersection of number.multipleOf constraints (adc98f9)
  • intersection: infinite loop (77b8621)
  • intersection: it should throw TypeError if unable to fake the given schema (5daa9b9)
  • intersection: it should throw when intersecting unrelated string schema (38e7849)
  • number: should generate valid data for multipleOf without int (a11d2e7)
  • promise: it should sometimes resolve on next microtask and task (1e2ab6f)
  • string: it should generate valid data when the string length constriant is smaller than the max or larger than the min (e9bf5c3)

1.3.2 (2025-02-02)

Bug Fixes

  • handle multiple checks of the same kind (#353) (5790523)

1.3.1 (2025-02-01)

Bug Fixes

  • any: prevent NaN genearetion as intersection of NaN is not allowed (51f680f)
  • interseciton: generate extra data when both objects are passthrough (b112e59)
  • intersection: throw error on conflicting multipleOf values of bigint (7d3f9b7)
  • intersection: throw error on conflicting string length limits (a675082)
  • string: ensure emoji adheres to length limits (8ed7d19)

1.3.0 (2025-01-31)

Features

1.2.2 (2025-01-16)

Bug Fixes

  • deps: update dependency @faker-js/faker to v9.4.0 (#331) (04a2621)

1.2.1 (2025-01-11)

Bug Fixes

  • should generate consistent values for z.date(), z.string().date(), z.string().datetime(), z.string().time() when seed (#328) (1f23704)

1.2.0 (2025-01-11)

Features

  • date edge case values should be generated occasionally, while more realistic values should be generated frequently (c223f46)

Bug Fixes

  • missing .catch (916cfaa)
  • set should sometimes generate a set which size equals min (63d0246)

1.1.3 (2025-01-05)

Bug Fixes

1.1.2 (2025-01-04)

Bug Fixes

1.1.1 (2025-01-03)

Bug Fixes

1.1.0 (2025-01-03)

Features

Bug Fixes

1.0.10 (2025-01-03)

Bug Fixes

1.0.9 (2024-12-15)

Bug Fixes

  • deps: update dependency zod to v3.24.1 (#290) (18676a5)

1.0.8 (2024-12-15)

Performance Improvements

1.0.7 (2024-12-03)

Bug Fixes

  • deps: update dependency @faker-js/faker to v9.3.0 (#286) (a6813e3)

1.0.6 (2024-11-30)

Bug Fixes

  • deps: update dependency @faker-js/faker to v9 (#229) (249f66f)

1.0.5 (2024-09-14)

Bug Fixes

1.0.4 (2024-09-02)

Bug Fixes

1.0.3 (2024-08-17)

Bug Fixes

  • readonly fake data should be freezed (#211) (0bab5a7)

1.0.2 (2024-08-16)

Bug Fixes

  • it should return undefined when fake z.void (#203) (ded1efa)
  • it should throws an error when fake z.never (#201) (d4e9652)

Performance Improvements

1.0.1 (2024-08-10)

Bug Fixes

1.0.0 (2024-06-30)

⚠ BREAKING CHANGES

  • migrate to vite (#152)

Features

Bug Fixes

Build System

0.2.0 (2024-04-22)

⚠ BREAKING CHANGES

  • use deps instead peer deps since there is no test for each versions

Features

Bug Fixes

build

  • use deps instead peer deps since there is no test for each versions (f33ace0)

0.1.6 (2023-01-25)

0.1.5 (2023-01-18)

0.1.4 (2022-12-21)

0.1.3 (2022-12-07)

0.1.2 (2022-04-09)

Features

  • add API descriptions (4731f8b)
  • add seed API (38e84b5)
  • generate random values of random schemas for any schema (c3f5ffc)
  • generate random values of random schemas for unknown schema (75e61ff)
  • remove faker, customization is not supported yet (1e3a48d)
  • remove internal API (514a4c8)
  • return random Date instead of now (2cf1a9f)

Bug Fixes

  • never type should not be used (14934ac)
  • seed not works as expected (4bc4b12)
  • void type could be any value (f3770c4)

0.1.1 (2022-04-08)

0.1.0 (2022-04-08)

Features