Created
February 25, 2026 11:57
-
-
Save carloitaben/0835c30a90d42d13a7c1f2a86c3f1931 to your computer and use it in GitHub Desktop.
unwrap zod object schema
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Removes wrappers such as effects and pipelines | |
| * and returns the inner object schema. | |
| */ | |
| function unwrapObjectSchema(schema: z.ZodTypeAny): z.AnyZodObject { | |
| if (schema instanceof z.ZodObject) { | |
| return schema | |
| } | |
| if (schema instanceof z.ZodEffects) { | |
| return unwrapObjectSchema(schema.innerType()) | |
| } | |
| if (schema instanceof z.ZodPipeline) { | |
| return unwrapObjectSchema(schema._def.out) | |
| } | |
| throw Error("Unsupported schema type", { cause: schema }) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment