@@ -27,23 +27,24 @@ interface FloatType {
2727interface BooleanType {
2828 type : 'boolean' ;
2929}
30+
31+ export type PrimitiveTypeSchema =
32+ | StringType
33+ | IntegerType
34+ | FloatType
35+ | BooleanType ;
36+
3037interface ArrayType {
3138 type : 'array' ;
3239 items : TypeSchema ; // Recursive
3340}
3441interface ObjectType {
3542 type : 'object' ;
36- additionalProperties ?: boolean | TypeSchema ; // Recursive
43+ additionalProperties ?: boolean | PrimitiveTypeSchema ;
3744}
3845
3946// 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 ;
4748
4849// The base properties of a named parameter.
4950interface BaseParameter {
@@ -55,6 +56,14 @@ interface BaseParameter {
5556
5657export type ParameterSchema = BaseParameter & TypeSchema ;
5758
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+
5867// Zod schema for the pure type definitions. This must be lazy for recursion.
5968const ZodTypeSchema : z . ZodType < TypeSchema > = z . lazy ( ( ) =>
6069 z . discriminatedUnion ( 'type' , [
@@ -65,7 +74,9 @@ const ZodTypeSchema: z.ZodType<TypeSchema> = z.lazy(() =>
6574 z . object ( { type : z . literal ( 'array' ) , items : ZodTypeSchema } ) ,
6675 z . object ( {
6776 type : z . literal ( 'object' ) ,
68- additionalProperties : z . union ( [ z . boolean ( ) , ZodTypeSchema ] ) . optional ( ) ,
77+ additionalProperties : z
78+ . union ( [ z . boolean ( ) , ZodPrimitiveTypeSchema ] )
79+ . optional ( ) ,
6980 } ) ,
7081 ] ) ,
7182) ;
@@ -113,7 +124,9 @@ function buildZodShapeFromTypeSchema(typeSchema: TypeSchema): ZodTypeAny {
113124 if ( typeof typeSchema . additionalProperties === 'object' ) {
114125 return z . record (
115126 z . string ( ) ,
116- buildZodShapeFromTypeSchema ( typeSchema . additionalProperties ) ,
127+ buildZodShapeFromTypeSchema (
128+ typeSchema . additionalProperties as TypeSchema ,
129+ ) ,
117130 ) ;
118131 } else if ( typeSchema . additionalProperties === false ) {
119132 return z . object ( { } ) ;
0 commit comments