Skip to content

Instantly share code, notes, and snippets.

@carloitaben
Created February 25, 2026 11:57
Show Gist options
  • Select an option

  • Save carloitaben/0835c30a90d42d13a7c1f2a86c3f1931 to your computer and use it in GitHub Desktop.

Select an option

Save carloitaben/0835c30a90d42d13a7c1f2a86c3f1931 to your computer and use it in GitHub Desktop.
unwrap zod object schema
/**
* 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