( schema: z.ZodTypeAny, paramsArray: Record<string, string[]> )
| 11 | } |
| 12 | |
| 13 | function processSchema( |
| 14 | schema: z.ZodTypeAny, |
| 15 | paramsArray: Record<string, string[]> |
| 16 | ): Record<string, any> { |
| 17 | if (schema instanceof z.ZodOptional) { |
| 18 | schema = schema._def.innerType; |
| 19 | } |
| 20 | switch (schema.constructor) { |
| 21 | case z.ZodObject: { |
| 22 | const shape = (schema as z.ZodObject<z.ZodRawShape>).shape; |
| 23 | return parseShape(shape, paramsArray); |
| 24 | } |
| 25 | case z.ZodUnion: { |
| 26 | const options = ( |
| 27 | schema as z.ZodUnion< |
| 28 | [z.ZodObject<z.ZodRawShape>, ...z.ZodObject<z.ZodRawShape>[]] |
| 29 | > |
| 30 | )._def.options; |
| 31 | for (const option of options) { |
| 32 | const shape = option.shape; |
| 33 | const requireds = getRequireds(shape); |
| 34 | |
| 35 | const result = parseShape(shape, paramsArray, true); |
| 36 | const keys = Object.keys(result); |
| 37 | |
| 38 | if (requireds.every((key) => keys.includes(key))) { |
| 39 | return result; |
| 40 | } |
| 41 | } |
| 42 | return {}; |
| 43 | } |
| 44 | default: |
| 45 | throw new Error("Unsupported schema type"); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | function getRequireds(shape: z.ZodRawShape) { |
| 50 | const keys: string[] = []; |
no test coverage detected