@@ -27,23 +27,24 @@ interface FloatType {
27
27
interface BooleanType {
28
28
type : 'boolean' ;
29
29
}
30
+
31
+ export type PrimitiveTypeSchema =
32
+ | StringType
33
+ | IntegerType
34
+ | FloatType
35
+ | BooleanType ;
36
+
30
37
interface ArrayType {
31
38
type : 'array' ;
32
39
items : TypeSchema ; // Recursive
33
40
}
34
41
interface ObjectType {
35
42
type : 'object' ;
36
- additionalProperties ?: boolean | TypeSchema ; // Recursive
43
+ additionalProperties ?: boolean | PrimitiveTypeSchema ;
37
44
}
38
45
39
46
// Union of all pure type definitions.
40
- export type TypeSchema =
41
- | StringType
42
- | IntegerType
43
- | FloatType
44
- | BooleanType
45
- | ArrayType
46
- | ObjectType ;
47
+ export type TypeSchema = PrimitiveTypeSchema | ArrayType | ObjectType ;
47
48
48
49
// The base properties of a named parameter.
49
50
interface BaseParameter {
@@ -55,6 +56,14 @@ interface BaseParameter {
55
56
56
57
export type ParameterSchema = BaseParameter & TypeSchema ;
57
58
59
+ const ZodPrimitiveTypeSchema : z . ZodType < PrimitiveTypeSchema > =
60
+ z . discriminatedUnion ( 'type' , [
61
+ z . object ( { type : z . literal ( 'string' ) } ) ,
62
+ z . object ( { type : z . literal ( 'integer' ) } ) ,
63
+ z . object ( { type : z . literal ( 'float' ) } ) ,
64
+ z . object ( { type : z . literal ( 'boolean' ) } ) ,
65
+ ] ) ;
66
+
58
67
// Zod schema for the pure type definitions. This must be lazy for recursion.
59
68
const ZodTypeSchema : z . ZodType < TypeSchema > = z . lazy ( ( ) =>
60
69
z . discriminatedUnion ( 'type' , [
@@ -65,7 +74,9 @@ const ZodTypeSchema: z.ZodType<TypeSchema> = z.lazy(() =>
65
74
z . object ( { type : z . literal ( 'array' ) , items : ZodTypeSchema } ) ,
66
75
z . object ( {
67
76
type : z . literal ( 'object' ) ,
68
- additionalProperties : z . union ( [ z . boolean ( ) , ZodTypeSchema ] ) . optional ( ) ,
77
+ additionalProperties : z
78
+ . union ( [ z . boolean ( ) , ZodPrimitiveTypeSchema ] )
79
+ . optional ( ) ,
69
80
} ) ,
70
81
] ) ,
71
82
) ;
@@ -113,7 +124,9 @@ function buildZodShapeFromTypeSchema(typeSchema: TypeSchema): ZodTypeAny {
113
124
if ( typeof typeSchema . additionalProperties === 'object' ) {
114
125
return z . record (
115
126
z . string ( ) ,
116
- buildZodShapeFromTypeSchema ( typeSchema . additionalProperties ) ,
127
+ buildZodShapeFromTypeSchema (
128
+ typeSchema . additionalProperties as TypeSchema ,
129
+ ) ,
117
130
) ;
118
131
} else if ( typeSchema . additionalProperties === false ) {
119
132
return z . object ( { } ) ;
0 commit comments