11import { ZodArray , ZodBigInt , ZodBoolean , ZodEnum , ZodNull , ZodNullable , ZodNumber , ZodObject , ZodOptional , ZodString , type ZodType , type ZodTypeDef , ZodUndefined , ZodUnion } from "zod" ;
22
3+ type ExampleGeneratorOptions = {
4+ keyName ?: string ,
5+ nullable : boolean ,
6+ } ;
7+
38export default function generateExample < I , O > (
49 schema : ZodType < O , ZodTypeDef , I > ,
510 ignoreOptionals : boolean ,
6- keyName ?: string ,
7- nullable : boolean = false ,
11+ generatorOptions : ExampleGeneratorOptions = { nullable : false } ,
812) : O {
913 if ( "defaultValue" in schema . _def && typeof schema . _def . defaultValue === "function" ) {
1014 return schema . _def . defaultValue ( ) as unknown as O ;
@@ -14,15 +18,15 @@ export default function generateExample<I, O>(
1418 if ( ignoreOptionals ) {
1519 return undefined as unknown as O ;
1620 }
17- return generateExample ( innerSchema , ignoreOptionals , keyName ) ;
21+ return generateExample ( innerSchema , ignoreOptionals , generatorOptions ) ;
1822 }
1923 if ( schema instanceof ZodNullable ) {
2024 const innerSchema = schema . unwrap ( ) ;
21- return ( generateExample ( innerSchema , ignoreOptionals , keyName , true ) ?? null ) as unknown as O ;
25+ return ( generateExample ( innerSchema , ignoreOptionals , { ... generatorOptions , nullable : true } ) ?? null ) as unknown as O ;
2226 }
2327 if ( schema instanceof ZodObject ) {
24- const entries = Object . entries ( schema . shape ) . map ( ( [ key , childSchema ] ) => {
25- return [ key , generateExample ( childSchema as ZodType , ignoreOptionals , key ) ] as const ;
28+ const entries = Object . entries ( schema . shape ) . map ( ( [ keyName , childSchema ] ) => {
29+ return [ keyName , generateExample ( childSchema as ZodType , ignoreOptionals , { ... generatorOptions , keyName } ) ] as const ;
2630 } ) . filter ( ( [ _ , value ] ) => typeof value !== "undefined" ) ;
2731 return Object . fromEntries ( entries ) as unknown as O ;
2832 }
@@ -91,26 +95,26 @@ export default function generateExample<I, O>(
9195 }
9296 return example . join ( "" ) as unknown as O ;
9397 }
94- if ( keyName ) {
95- return `example ${ keyName } ` as unknown as O ;
98+ if ( generatorOptions . keyName ) {
99+ return `example ${ generatorOptions . keyName } ` as unknown as O ;
96100 }
97- if ( nullable ) {
101+ if ( generatorOptions . nullable ) {
98102 return "example nullable string" as unknown as O ;
99103 }
100104 return "example string" as unknown as O ;
101105 }
102106 if ( schema instanceof ZodArray ) {
103107 if ( schema . _def . type instanceof ZodUnion ) {
104- const options = schema . _def . type . options as ZodType [ ] ;
105- return options . map ( option => generateExample ( option , ignoreOptionals ) ) as unknown as O ;
108+ const unionOptions = schema . _def . type . options as ZodType [ ] ;
109+ return unionOptions . map ( o => generateExample ( o , ignoreOptionals , generatorOptions ) ) as unknown as O ;
106110 }
107- return [ generateExample ( schema . _def . type , ignoreOptionals ) ] as unknown as O ;
111+ return [ generateExample ( schema . _def . type , ignoreOptionals , generatorOptions ) ] as unknown as O ;
108112 }
109113 if ( schema instanceof ZodUnion ) {
110- const options = schema . options as ZodType [ ] ;
111- const firstItem = options [ 0 ] ;
114+ const unionOptions = schema . options as ZodType [ ] ;
115+ const firstItem = unionOptions [ 0 ] ;
112116 if ( firstItem ) {
113- return generateExample ( firstItem , ignoreOptionals ) ;
117+ return generateExample ( firstItem , ignoreOptionals , generatorOptions ) ;
114118 }
115119 return undefined as unknown as O ;
116120 }
0 commit comments