@@ -132,52 +132,47 @@ function generateZodSchemasForFunc(func) {
132
132
overloads = funcInfo . overloads ;
133
133
}
134
134
135
- // Generate a schema for a single parameter that can be of multiple constants.
136
- // Note that because we're ultimately interested in the value of the constant,
137
- // mapping constants to their values via `constantsMap` is necessary.
138
- //
139
- // Also, z.num() can only be used for a fixed set of allowable string values.
140
- // However, our constants sometimes have numeric or non-primitive values.
141
- // Therefore, z.union() is used here instead.
142
- const generateConstantSchema = constants => {
143
- return z . union ( constants . map ( c => z . literal ( constantsMap [ c ] ) ) ) ;
144
- }
145
-
146
- // Generate a schema for a single parameter that can be of multiple
147
- // non-constant types, i.e. `String|Number|Array`.
148
- const generateUnionSchema = types => {
149
- return z . union ( types
150
- . filter ( t => {
151
- if ( ! schemaMap [ t ] ) {
152
- console . log ( `Warning: Zod schema not found for type '${ t } '. Skip mapping` ) ;
153
- return false ;
154
- }
155
- return true ;
156
- } )
157
- . map ( t => schemaMap [ t ] ) ) ;
158
- }
135
+ // Returns a schema for a single type, i.e. z.boolean() for `boolean`.
136
+ const generateTypeSchema = type => {
137
+ // Type only contains uppercase letters and underscores -> type is a
138
+ // constant. Note that because we're ultimately interested in the value of
139
+ // the constant, mapping constants to their values via `constantsMap` is
140
+ // necessary.
141
+ if ( / ^ [ A - Z _ ] + $ / . test ( type ) ) {
142
+ return z . literal ( constantsMap [ type ] ) ;
143
+ } else if ( schemaMap [ type ] ) {
144
+ return schemaMap [ type ] ;
145
+ } else {
146
+ // TODO: Make this throw an error once more types are supported.
147
+ console . log ( `Warning: Zod schema not found for type '${ type } '. Skip mapping` ) ;
148
+ return undefined ;
149
+ }
150
+ } ;
159
151
160
- // Generate a schema for a single parameter.
152
+ // Generate a schema for a single parameter. In the case where a parameter can
153
+ // be of multiple types, `generateTypeSchema` is called for each type.
161
154
const generateParamSchema = param => {
162
155
const optional = param . endsWith ( '?' ) ;
163
156
param = param . replace ( / \? $ / , '' ) ;
164
157
165
158
let schema ;
166
159
167
- // The parameter is can be of multiple types / constants
160
+ // Generate a schema for a single parameter that can be of multiple
161
+ // types / constants, i.e. `String|Number|Array`.
162
+ //
163
+ // Here, z.union() is used over z.enum() (which seems more intuitive) for
164
+ // constants for the following reasons:
165
+ // 1) z.enum() only allows a fixed set of allowable string values. However,
166
+ // our constants sometimes have numeric or non-primitive values.
167
+ // 2) In some cases, the type can be constants or strings, making z.enum()
168
+ // insufficient for the use case.
168
169
if ( param . includes ( '|' ) ) {
169
170
const types = param . split ( '|' ) ;
170
-
171
- // Note that for parameter types that are constants, such as for
172
- // `blendMode`, the parameters are always all caps, sometimes with
173
- // underscores, separated by `|` (i.e. "BLEND|DARKEST|LIGHTEST").
174
- // Use a regex check here to filter them out and distinguish them from
175
- // parameters that allow multiple types.
176
- schema = types . every ( t => / ^ [ A - Z _ ] + $ / . test ( t ) )
177
- ? generateConstantSchema ( types )
178
- : generateUnionSchema ( types ) ;
171
+ schema = z . union ( types
172
+ . map ( t => generateTypeSchema ( t ) )
173
+ . filter ( s => s !== undefined ) ) ;
179
174
} else {
180
- schema = schemaMap [ param ] ;
175
+ schema = generateTypeSchema ( param ) ;
181
176
}
182
177
183
178
return optional ? schema . optional ( ) : schema ;
0 commit comments