| 223 | // Generate a schema for a single parameter. In the case where a parameter can |
| 224 | // be of multiple types, `generateTypeSchema` is called for each type. |
| 225 | const generateParamSchema = param => { |
| 226 | const isOptional = param?.endsWith('?'); |
| 227 | param = param?.replace(/\?$/, ''); |
| 228 | |
| 229 | const isRest = param?.startsWith('...') && param?.endsWith('[]'); |
| 230 | param = param?.replace(/^\.\.\.(.+)\[\]$/, '$1'); |
| 231 | |
| 232 | let schema = generateTypeSchema(param); |
| 233 | // Fallback to z.custom() because function types are no longer |
| 234 | // returns a Zod schema. |
| 235 | if (schema.def.type === 'function') { |
| 236 | schema = z.custom(val => val instanceof Function); |
| 237 | } |
| 238 | |
| 239 | if (isOptional) { |
| 240 | schema = schema.optional(); |
| 241 | } |
| 242 | return { schema, rest: isRest }; |
| 243 | }; |
| 244 | |
| 245 | // Note that in Zod, `optional()` only checks for undefined, not the absence |
| 246 | // of value. |
no test coverage detected