@@ -2,6 +2,7 @@ import { ZodSchema } from 'zod';
2
2
import { Options , Targets } from './Options' ;
3
3
import { JsonSchema7Type , parseDef } from './parseDef' ;
4
4
import { getRefs } from './Refs' ;
5
+ import { zodDef , isEmptyObj } from './util' ;
5
6
6
7
const zodToJsonSchema = < Target extends Targets = 'jsonSchema7' > (
7
8
schema : ZodSchema < any > ,
@@ -16,25 +17,6 @@ const zodToJsonSchema = <Target extends Targets = 'jsonSchema7'>(
16
17
} => {
17
18
const refs = getRefs ( options ) ;
18
19
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
-
38
20
const name =
39
21
typeof options === 'string' ? options
40
22
: options ?. nameStrategy === 'title' ? undefined
@@ -61,6 +43,25 @@ const zodToJsonSchema = <Target extends Targets = 'jsonSchema7'>(
61
43
main . title = title ;
62
44
}
63
45
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
+
64
65
const combined : ReturnType < typeof zodToJsonSchema < Target > > =
65
66
name === undefined ?
66
67
definitions ?
0 commit comments