Gathering detailed insights and metrics for zod-schema-faker
Gathering detailed insights and metrics for zod-schema-faker
Gathering detailed insights and metrics for zod-schema-faker
Gathering detailed insights and metrics for zod-schema-faker
Generates mock data from zod schema. Powered by @faker-js/faker and randexp.js
npm install zod-schema-faker
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
17 Stars
306 Commits
2 Watching
8 Branches
1 Contributors
Updated on 28 Nov 2024
Minified
Minified + Gzipped
TypeScript (93.87%)
JavaScript (5.84%)
HTML (0.29%)
Cumulative downloads
Total Downloads
Last day
16.9%
76
Compared to previous day
Last week
17.7%
413
Compared to previous week
Last month
-0.1%
1,812
Compared to previous month
Last year
811.7%
7,613
Compared to previous year
2
1
Generates mock data from zod schema. Powered by @faker-js/faker and randexp.js
fake(z.any())
1npm install --save-dev zod-schema-faker
1import * as z from 'zod' 2import { install, fake } from 'zod-schema-faker' 3 4const schema = z.number() 5 6// enable tree shaking 7if (process.env.NODE_ENV === 'development') { 8 install() 9 10 const data = fake(schema) 11 12 console.log(data) // => -2556.9 13}
1interface Category { 2 name: string 3 subcategories: Category[] 4} 5const category = z.lazy(() => 6 z.object({ 7 name: z.string(), 8 subcategories: z.array(category), 9 }), 10) as z.ZodType<Category> 11 12const schema = z.object({ 13 primitives: z.object({ 14 string: z.string(), 15 number: z.number(), 16 bigint: z.bigint(), 17 boolean: z.boolean(), 18 date: z.date(), 19 }), 20 emptyValues: z.object({ 21 undefined: z.undefined(), 22 null: z.null(), 23 void: z.void(), 24 }), 25 any: z.any(), 26 unknown: z.unknown(), 27 literal: z.literal('tuna'), 28 strings: z.object({ 29 max: z.string().max(5), 30 min: z.string().min(5), 31 length: z.string().length(5), 32 email: z.string().email(), 33 url: z.string().url(), 34 uuid: z.string().uuid(), 35 cuid: z.string().cuid(), 36 regex: z.string().regex(/hello+ (world|to you)/), 37 }), 38 numbers: z.object({ 39 gt: z.number().gt(5), 40 gte: z.number().gte(5), 41 lt: z.number().lt(5), 42 lte: z.number().lte(5), 43 int: z.number().int(), 44 positive: z.number().positive(), 45 nonnegative: z.number().nonnegative(), 46 negative: z.number().negative(), 47 nonpositive: z.number().nonpositive(), 48 multipleOf: z.number().multipleOf(42), 49 }), 50 nan: z.nan(), 51 boolean: z.boolean(), 52 date: z.date(), 53 enum: z.enum(['Salmon', 'Tuna', 'Trout']), 54 nativeEnum: z.nativeEnum({ 55 Apple: 'apple', 56 Banana: 'banana', 57 Cantaloupe: 3, 58 } as const), 59 optional: z.optional(z.string()), 60 nullable: z.nullable(z.string()), 61 object: z.object({ 62 name: z.string(), 63 age: z.number(), 64 }), 65 array: z.array(z.string()), 66 tuple: z.tuple([ 67 z.string(), 68 z.number(), 69 z.object({ 70 pointsScored: z.number(), 71 }), 72 ]), 73 union: z.union([z.string(), z.number()]), 74 discriminatedUnions: z.discriminatedUnion('type', [ 75 z.object({ type: z.literal('a'), a: z.string() }), 76 z.object({ type: z.literal('b'), b: z.string() }), 77 ]), 78 record: z.record(z.string(), z.number()), 79 map: z.map(z.string(), z.number()), 80 set: z.set(z.number()), 81 lazy: category, 82 promise: z.promise(z.number()), 83})
1const data = { 2 primitives: { 3 string: 'velit ullam blanditiis minus omnis enim ut repellat neque iure nisi ipsam d', 4 number: 235709817303039.3, 5 bigint: 4848309999951517n, 6 boolean: true, 7 date: 2022-04-08T08:31:40.690Z, 8 }, 9 emptyValues: { 10 undefined: undefined, 11 null: null, 12 void: undefined, 13 }, 14 any: undefined, 15 unknown: undefined, 16 literal: 'tuna', 17 strings: { 18 max: 'expli', 19 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', 20 length: 'natus', 21 email: 'Macey.Stoltenberg@gmail.com', 22 url: 'https://fabulous-straw.name', 23 uuid: '76bac9fd-b1b7-49d0-a600-2d4711531986', 24 cuid: "c&BL3'_Vrk1G+3G!wwjn3cu*8|b>_Cf{", 25 regex: 'hellooooooooooooooooooooooooooooooooooooooooo world' 26 }, 27 numbers: { 28 gt: 5328.419054, 29 gte: 36.218591, 30 lt: -1628167036925.9614, 31 lte: -27882354.519148372, 32 int: 16636929, 33 positive: 531694153629695.06, 34 nonnegative: 579.4830352, 35 negative: -38.025, 36 nonpositive: -1725.5520665581, 37 multipleOf: 84, 38 }, 39 nan: NaN, 40 boolean: false, 41 date: 2022-04-08T08:31:40.697Z, 42 enum: 'Salmon', 43 nativeEnum: 3, 44 optional: 'elige', 45 nullable: null, 46 object: { 47 name: 'voluptas assumenda odit eveniet cum aut ut doloremque iusto animi modi et q', 48 age: -44363342.91183093 49 }, 50 array: [], 51 tuple: [ 'quide', 421341404834213.9, { pointsScored: 15651503922295.52 } ], 52 union: -1965623492324.935, 53 discriminatedUnions: { type: 'b', b: 'unde ' }, 54 record: { 55 '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 56 }, 57 map: Map(1) { 'vel e' => 2732936928215370 }, 58 set: Set(1) { 25431.81674167793 }, 59 intersection: { 60 name: 'earum consequatur sit maiores eius hic autem blanditiis aut ut dolore iusto', 61 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' 62 }, 63 lazy: { 64 name: 'provident omnis ut ex blanditiis aut et minima nobis enim qui et placeat es', 65 subcategories: [ [Object], [Object] ] 66 }, 67 promise: Promise { -37512948614638.48 } 68}
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
1import * as z from 'zod' 2import { install, fake, seed } from 'zod-schema-faker' 3 4install() 5 6const schema = z.number() 7 8seed(1) 9console.log(fake(schema)) // => 8399729968525060 10seed(1) 11console.log(fake(schema)) // => 8399729968525060 12console.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
Distributed under the MIT license. See LICENSE for more information.
No vulnerabilities found.
No security vulnerabilities found.