File tree Expand file tree Collapse file tree 7 files changed +35
-26
lines changed Expand file tree Collapse file tree 7 files changed +35
-26
lines changed Original file line number Diff line number Diff line change @@ -25,17 +25,18 @@ Built-in zod types:
2525``` ts
2626import * as z from ' zod' // or import * as z from 'zod/mini'
2727import { faker } from ' @faker-js/faker'
28- import { fake , fakeSchema , setFaker } from ' zod-schema-faker'
28+ import { fake , setFaker } from ' zod-schema-faker'
2929
30- const User = z .interface ({
31- name : z .string (),
32- age : z .uint32 (),
30+ const Player = z .object ({
31+ username : z .string (),
32+ xp : z .number (),
3333})
3434
3535// enable tree shaking
3636if (process .env .NODE_ENV === ' development' ) {
37- setFaker (faker ) // or setFaker(your faker instance)
38- fake (z .string ()) // { name: 'lorem', age: 42 }
37+ setFaker (faker )
38+ const data = fake (Player )
39+ console .log (data ) // { username: "billie", xp: 100 }
3940}
4041```
4142
@@ -88,11 +89,9 @@ fake(px) // '100px'
8889## Unsupported
8990
9091- .file 🚧
91- - .function 🚧
9292- .intersection 🚧
9393- .refine ❌
9494- .superRefine ❌
95- - .transform 🚧
9695
9796## About
9897
Original file line number Diff line number Diff line change @@ -3,15 +3,17 @@ import { expectType, TypeEqual } from 'ts-expect'
33import { z } from 'zod'
44import { fake , setFaker } from 'zod-schema-faker'
55
6- const schema = z . number ( )
6+ const Player = z . object ( {
7+ username : z . string ( ) ,
8+ xp : z . number ( ) ,
9+ } )
710
811// enable tree shaking
912if ( process . env . NODE_ENV === 'development' ) {
1013 setFaker ( faker )
11- const data = fake ( schema )
12-
13- console . log ( data ) // => -2556.9
14+ const data = fake ( Player )
15+ console . log ( data ) // { username: "billie", xp: 100 }
1416
1517 expectType < TypeEqual < typeof data , any > > ( false )
16- expectType < TypeEqual < typeof data , number > > ( true )
18+ expectType < TypeEqual < typeof data , { username : string ; xp : number } > > ( true )
1719}
Original file line number Diff line number Diff line change 1+ import * as core from 'zod/v4/core'
12import { rootFake } from './internals/fake'
2- import { ZodType } from './internals/type'
33
4- export function fake < T extends ZodType > ( schema : T ) : T [ '_zod' ] [ 'output' ] {
4+ export function fake < T extends core . $ ZodType> ( schema : T ) : core . infer < T > {
55 return rootFake ( schema , { depth : 0 } )
66}
Original file line number Diff line number Diff line change 11import * as core from 'zod/v4/core'
22import { Context } from '../context'
3- import { Fake , Infer , RootFake , ZodType } from '../type'
3+ import { Fake , Infer , RootFake } from '../type'
44
5- const customs = new Map < ZodType , Fake < ZodType > > ( )
6- export function custom < T extends ZodType > ( schema : T , fake : Fake < T > ) : void {
5+ const customs = new Map < core . $ ZodType, Fake < core . $ ZodType> > ( )
6+ export function custom < T extends core . $ ZodType> ( schema : T , fake : Fake < T > ) : void {
77 customs . set ( schema , fake as any )
88}
99
Original file line number Diff line number Diff line change 1+ import * as core from 'zod/v4/core'
12import { Context } from './context'
23
3- export type ZodType < O = unknown > = { _zod : { output : O } } // TODO: use core.$ZodType
4+ export type Infer < T extends core . $ZodType > = T [ ' _zod' ] [ ' output' ]
45
5- export type Infer < T extends ZodType > = T [ '_zod' ] [ 'output' ]
6+ export type Fake < T extends core . $ ZodType> = ( schema : T , context : Context , fake : RootFake ) => core . infer < T >
67
7- export type Fake < T extends ZodType > = ( schema : T , context : Context , fake : RootFake ) => Infer < T >
8-
9- export type RootFake = < T extends ZodType > ( schema : T , context : Context ) => Infer < T >
8+ export type RootFake = < T extends core . $ZodType > ( schema : T , context : Context ) => core . infer < T >
Original file line number Diff line number Diff line change @@ -596,8 +596,14 @@ const validSuits: { schema: z.ZodMiniType; description?: string; only?: boolean;
596596 description : 'refinement' ,
597597 } ,
598598
599- // TODO: transform
600- // { schema: z.transform(val => String(val)) },
599+ // transform
600+ {
601+ schema : z . pipe (
602+ z . string ( ) ,
603+ z . transform ( val => val . length ) ,
604+ ) ,
605+ description : 'transform' ,
606+ } ,
601607
602608 // tuple
603609 {
Original file line number Diff line number Diff line change @@ -598,7 +598,10 @@ const validSuits: { schema: z.ZodType; description?: string; only?: boolean; asy
598598 } ,
599599
600600 // TODO: transform
601- // { schema: z.transform(val => String(val)) },
601+ {
602+ schema : z . string ( ) . pipe ( z . transform ( val => val . length ) ) ,
603+ description : 'transform' ,
604+ } ,
602605
603606 // tuple
604607 {
You can’t perform that action at this time.
0 commit comments