npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

zod-schema-faker

v1.0.9

Published

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

Downloads

1,801

Readme

zod-schema-faker

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

zod-schema-faker version

Supported APIs

  • ✅ z.any
  • ✅ z.array
  • z.bigint
    • ✅ gt
    • ✅ gte
    • ✅ lt
    • ✅ lte
    • ✅ positive
    • ✅ nonnegative
    • ✅ negative
    • ✅ nonpositive
    • ✅ multipleOf
  • ✅ z.boolean
  • ✅ z.custom: see example for details.
  • z.date
    • ✅ min
    • ✅ max
  • ✅ z.default
  • ✅ z.discriminatedUnion
  • z.effects
    • ✅ preprocess[^2]
    • ❌ refine: not yet supported.
    • ✅ transform: works as expected.
  • ✅ z.enum
  • ✅ z.function
  • ❌ z.intersection
  • ✅ z.lazy
  • ✅ z.literal
  • ✅ z.map
  • ✅ z.nan
  • z.nativeEnum
    • ✅ numeric enums
    • ✅ string enums
    • ✅ const enums
  • ✅ z.never: always throws an error
  • ✅ z.null
  • ✅ z.nullable
  • z.number
    • ✅ gt
    • ✅ gte
    • ✅ lt
    • ✅ lte
    • ✅ int
    • ✅ positive
    • ✅ nonnegative
    • ✅ negative
    • ✅ nonpositive
    • ✅ multipleOf
    • ✅ finite
    • ✅ safe
  • ✅ z.object
  • ✅ z.optional
  • ✅ z.pipe
  • ✅ z.promise
  • ✅ z.readonly
  • ✅ z.record
  • ✅ z.set
  • z.string
    • ✅ base64[^1]
    • ✅ base64url[^1]
    • ✅ cidr[^1]
    • ✅ cuid[^1]
    • ✅ cuid2[^1]
    • ✅ date[^1]
    • ✅ datetime[^1]
    • ✅ duration[^1]
    • ✅ email[^1]
    • ✅ emoji
    • ✅ endsWith
    • ✅ includes
    • ✅ ip[^1]
    • ✅ jwt[^1]
    • ✅ length
    • ✅ max
    • ✅ min
    • ✅ nanoid[^1]
    • ✅ regex[^1]
    • ✅ startsWith
    • ✅ time[^1]
    • ✅ toLowerCase
    • ✅ toUpperCase
    • ✅ trim
    • ✅ ulid[^1]
    • ✅ url[^1]
    • ✅ uuid[^1]
  • ✅ z.tuple
  • ✅ z.undefined
  • ✅ z.union
  • ✅ z.unknown: returns fake(z.any())
  • ✅ z.void

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

[^2]: Not applicable, ignored

Installation

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

Usage

import * as z from 'zod'
import { install, fake } from 'zod-schema-faker'

const schema = z.number()

// enable tree shaking
if (process.env.NODE_ENV === 'development') {
  install()

  const data = fake(schema)

  console.log(data) // => -2556.9
}

Full Example

interface Category {
  name: string
  subcategories: Category[]
}
const category = z.lazy(() =>
  z.object({
    name: z.string(),
    subcategories: z.array(category),
  }),
) as z.ZodType<Category>

const schema = z.object({
  primitives: z.object({
    string: z.string(),
    number: z.number(),
    bigint: z.bigint(),
    boolean: z.boolean(),
    date: z.date(),
  }),
  emptyValues: z.object({
    undefined: z.undefined(),
    null: z.null(),
    void: z.void(),
  }),
  any: z.any(),
  unknown: z.unknown(),
  literal: z.literal('tuna'),
  strings: z.object({
    max: z.string().max(5),
    min: z.string().min(5),
    length: z.string().length(5),
    email: z.string().email(),
    url: z.string().url(),
    uuid: z.string().uuid(),
    cuid: z.string().cuid(),
    regex: z.string().regex(/hello+ (world|to you)/),
  }),
  numbers: z.object({
    gt: z.number().gt(5),
    gte: z.number().gte(5),
    lt: z.number().lt(5),
    lte: z.number().lte(5),
    int: z.number().int(),
    positive: z.number().positive(),
    nonnegative: z.number().nonnegative(),
    negative: z.number().negative(),
    nonpositive: z.number().nonpositive(),
    multipleOf: z.number().multipleOf(42),
  }),
  nan: z.nan(),
  boolean: z.boolean(),
  date: z.date(),
  enum: z.enum(['Salmon', 'Tuna', 'Trout']),
  nativeEnum: z.nativeEnum({
    Apple: 'apple',
    Banana: 'banana',
    Cantaloupe: 3,
  } as const),
  optional: z.optional(z.string()),
  nullable: z.nullable(z.string()),
  object: z.object({
    name: z.string(),
    age: z.number(),
  }),
  array: z.array(z.string()),
  tuple: z.tuple([
    z.string(),
    z.number(),
    z.object({
      pointsScored: z.number(),
    }),
  ]),
  union: z.union([z.string(), z.number()]),
  discriminatedUnions: z.discriminatedUnion('type', [
    z.object({ type: z.literal('a'), a: z.string() }),
    z.object({ type: z.literal('b'), b: z.string() }),
  ]),
  record: z.record(z.string(), z.number()),
  map: z.map(z.string(), z.number()),
  set: z.set(z.number()),
  lazy: category,
  promise: z.promise(z.number()),
})
const data = {
  primitives: {
    string: 'velit ullam blanditiis minus omnis enim ut repellat neque iure nisi ipsam d',
    number: 235709817303039.3,
    bigint: 4848309999951517n,
    boolean: true,
    date: 2022-04-08T08:31:40.690Z,
  },
  emptyValues: {
    undefined: undefined,
    null: null,
    void: undefined,
  },
  any: undefined,
  unknown: undefined,
  literal: 'tuna',
  strings: {
    max: 'expli',
    min: 'voluptas dolor repellendus recusandae blanditiis deleniti labore ut voluptates ea quas accusantium et reiciendis at et cumque nihil sit tempore molestias quaerat non molestiae ex sit necessitatibus ipsum fuga est repellendus natus est esse cupiditate dolorem numquam voluptate consequatur et non nemo architecto quia et aut maxime voluptates suscipit similique provident id fuga iure debitis magni repudiandae est dolorem illo totam et voluptatem nihil sit molestiae laboriosam aut possimus tempore minus illo illum magni nihil dolores atque quam sint praesentium laborum sed non molestias eius quisquam quibusdam quas est deserunt quis asperiores laborum accusantium illum temporibus id atque tempora minima molestias quaerat sit voluptatem et iure autem neque perferendis sequi totam nesciunt voluptas et qui praesentium aliquid eaque dolorem voluptatem hic dolorem iure in est aut distinctio repudiandae perferendis amet consequatur quibusdam et eos tenetur nesciunt et iusto atque debitis commodi',
    length: 'natus',
    email: '[email protected]',
    url: 'https://fabulous-straw.name',
    uuid: '76bac9fd-b1b7-49d0-a600-2d4711531986',
    cuid: "c&BL3'_Vrk1G+3G!wwjn3cu*8|b>_Cf{",
    regex: 'hellooooooooooooooooooooooooooooooooooooooooo world'
  },
  numbers: {
    gt: 5328.419054,
    gte: 36.218591,
    lt: -1628167036925.9614,
    lte: -27882354.519148372,
    int: 16636929,
    positive: 531694153629695.06,
    nonnegative: 579.4830352,
    negative: -38.025,
    nonpositive: -1725.5520665581,
    multipleOf: 84,
  },
  nan: NaN,
  boolean: false,
  date: 2022-04-08T08:31:40.697Z,
  enum: 'Salmon',
  nativeEnum: 3,
  optional: 'elige',
  nullable: null,
  object: {
    name: 'voluptas assumenda odit eveniet cum aut ut doloremque iusto animi modi et q',
    age: -44363342.91183093
  },
  array: [],
  tuple: [ 'quide', 421341404834213.9, { pointsScored: 15651503922295.52 } ],
  union: -1965623492324.935,
  discriminatedUnions: { type: 'b', b: 'unde ' },
  record: {
    'aperiam et sit dolor et in ratione aut qui et sequi quisquam culpa quia exercitationem quas commodi sit amet sequi ipsum voluptas qui consectetur optio odio iusto et corrupti modi corrupti quasi sunt id ut cumque ut hic tempora ut accusamus doloremque est aut commodi delectus excepturi accusantium ut quam est asperiores aspernatur fuga hic dolor alias autem quaerat dolores laborum aut dicta consequuntur natus provident rerum asperiores ipsam sint perspiciatis quia quia praesentium quia placeat voluptatem magnam necessitatibus impedit commodi nulla voluptatum cupiditate quasi optio velit numquam beatae et quaerat in odit officia porro rem illum blanditiis est rerum unde exercitationem dolores ut ut aliquid quidem laborum iste suscipit saepe temporibus rerum facere eum culpa laborum ducimus ipsa sequi voluptas est qui molestiae occaecati vel est quia consequatur rerum ad veniam corrupti exercitationem eius libero ea temporibus aut occaecati qui eaque facere ipsam sint expedita ut aut tem': 123095259.95589001
  },
  map: Map(1) { 'vel e' => 2732936928215370 },
  set: Set(1) { 25431.81674167793 },
  intersection: {
    name: 'earum consequatur sit maiores eius hic autem blanditiis aut ut dolore iusto',
    role: 'quis eaque maxime ipsum aut tempore id repellendus sint velit voluptate tempora aut pariatur quasi unde aut optio deleniti optio voluptas est laboriosam quasi aut debitis reprehenderit quia voluptas sit vel sit maiores voluptas vitae culpa commodi aperiam voluptatem modi quia iusto maiores odit quod vel sunt magnam voluptatum eius maiores maxime numquam consequatur hic hic autem accusantium eum veritatis rem quia omnis voluptatibus libero qui harum eum labore molestiae molestias doloribus dolorem mollitia sunt minima consequatur veritatis animi enim neque aut velit nostrum vel ut esse at error illum error eveniet omnis sed et consequatur autem omnis quam esse eum qui tenetur aliquid harum dolorum sit et voluptatem recusandae quo provident dolorum earum doloremque voluptatem dolor quis in minima sint accusantium sequi optio culpa iste minima in officia illum ut optio sapiente expedita facilis aut sunt perspiciatis non modi aut molestiae quo molestiae nemo architecto pariatur doloribus s'
  },
  lazy: {
    name: 'provident omnis ut ex blanditiis aut et minima nobis enim qui et placeat es',
    subcategories: [ [Object], [Object] ]
  },
  promise: Promise { -37512948614638.48 }
}

API

  • function install(): register fakers, must be called before using fake()

  • function installCustom(schema, faker): register custom fakers for custom zod types, must be called before using fake()

  • function fake(schema): generate fake data based on schema

    This function may throw ZodSchemaFakerError if a valid value cannot be generated for the Zod schema, or if the schema is not supported.

  • function seed(value): sets the seed to use

    import * as z from 'zod'
    import { install, fake, seed } from 'zod-schema-faker'
    
    install()
    
    const schema = z.number()
    
    seed(1)
    console.log(fake(schema)) // => 8399729968525060
    seed(1)
    console.log(fake(schema)) // => 8399729968525060
    console.log(fake(schema)) // => 62.93956000000001
  • function randexp(pattern, flags): see example for details

  • function runFake(runner): see example for details

  • class ZodTypeFaker: see example for details

  • class ZodSchemaFakerError

About

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