@@ -2,6 +2,7 @@ import { ZodSchema } from 'zod';
22import { Options , Targets } from './Options' ;
33import { JsonSchema7Type , parseDef } from './parseDef' ;
44import { getRefs } from './Refs' ;
5+ import { zodDef , isEmptyObj } from './util' ;
56
67const zodToJsonSchema = < Target extends Targets = 'jsonSchema7' > (
78 schema : ZodSchema < any > ,
@@ -16,25 +17,6 @@ const zodToJsonSchema = <Target extends Targets = 'jsonSchema7'>(
1617} => {
1718 const refs = getRefs ( options ) ;
1819
19- const definitions =
20- typeof options === 'object' && options . definitions ?
21- Object . entries ( options . definitions ) . reduce (
22- ( acc , [ name , schema ] ) => ( {
23- ...acc ,
24- [ name ] :
25- parseDef (
26- schema . _def ,
27- {
28- ...refs ,
29- currentPath : [ ...refs . basePath , refs . definitionPath , name ] ,
30- } ,
31- true ,
32- ) ?? { } ,
33- } ) ,
34- { } ,
35- )
36- : undefined ;
37-
3820 const name =
3921 typeof options === 'string' ? options
4022 : options ?. nameStrategy === 'title' ? undefined
@@ -61,6 +43,25 @@ const zodToJsonSchema = <Target extends Targets = 'jsonSchema7'>(
6143 main . title = title ;
6244 }
6345
46+ const definitions =
47+ ! isEmptyObj ( refs . definitions ) ?
48+ Object . entries ( refs . definitions ) . reduce (
49+ ( acc , [ name , schema ] ) => ( {
50+ ...acc ,
51+ [ name ] :
52+ parseDef (
53+ zodDef ( schema ) ,
54+ {
55+ ...refs ,
56+ currentPath : [ ...refs . basePath , refs . definitionPath , name ] ,
57+ } ,
58+ true ,
59+ ) ?? { } ,
60+ } ) ,
61+ { } ,
62+ )
63+ : undefined ;
64+
6465 const combined : ReturnType < typeof zodToJsonSchema < Target > > =
6566 name === undefined ?
6667 definitions ?
0 commit comments