Merging into a schema that accepts "any type of these 3" is done by a union of the three types, using ….or(…) or z.union(…). However, since those types are to be distinguished based on their attachmentType, you really want a discriminated union which will make checking more efficient:

const postSchema = z.discriminatedUnion("attachmentType", [
  imagePostSchema,
  videoPostSchema,
  textPostSchema,
]);
Answer from Bergi on Stack Overflow
🌐
Zod
zod.dev › api
Defining schemas | Zod
When merging object schemas, prefer A.extend(B) over intersections. Using .extend() will give you a new object schema, whereas z.intersection(A, B) returns a ZodIntersection instance which lacks common object methods like pick and omit.
Discussions

How to combine form zod schemas
Create a parametrized function that returns an extended schema based on the parameter received. import { z } from "zod"; const SaveSchema = (operation: "create" | "update") => z.object({ ...operation === "create" && { username: z.string() }, email: z.string(), }); In this case, you can both create and update your email, but the username is not editable. More on reddit.com
🌐 r/nextjs
4
2
January 15, 2024
How to union or merge two or more z.record schemas
Thanks for the great library, really finding it useful. Is there a recommended way to define a union of two or more record schemas, that would successfully parse an object with a mix of key/values ... More on github.com
🌐 github.com
3
September 6, 2024
How do I merge two Zod schemas?
Hey guys, I am creating a form with react-hook-form and Zod. The form was too long, so I decided to divide it into multi step form, with each step having separate Zod schema. I am storing the form data locally until all steps are complete. Now, I want a Zod schema for combining the schemas ... More on answeroverflow.com
🌐 answeroverflow.com
June 16, 2024
Calling `.merge` or `.extend` on recursive types
First off, really impressive project! So my use case is probably not a great fit for zod, but I'd like to try anyway. I'm struggling with trying to define a schema that involves deeply recu... More on github.com
🌐 github.com
1
1
🌐
GitHub
github.com › colinhacks › zod › issues › 5294
Merging multiple schema and preserving refine / transform for each · Issue #5294 · colinhacks/zod
September 30, 2025 - The idea being, that each Zod final entity combine all the 3 and A & C are common modules imported and re-used all over.
Author   colinhacks
🌐
DEV Community
dev.to › shaharke › schema-extension-and-reuse-clc
Schema Extension and Reuse - Zod
March 30, 2024 - Zod also allows us to merge multiple schemas together using the .merge() method.
🌐
Pic2Map
basicutils.com › learn › zod › zod-intersection-merging-types-with-precision
Understanding Zod Intersection: Merging Types with Precision
October 20, 2024 - Zod.intersection is a method that allows you to combine two or more Zod schemas into a single schema. This merged schema requires the validated data to satisfy all of the combined schemas, effectively creating a new type that encompasses the ...
🌐
GitHub
v3.zod.dev
Zod | Documentation
Though in many cases, it is recommended to use A.merge(B) to merge two objects. The .merge method returns a new ZodObject instance, whereas A.and(B) returns a less useful ZodIntersection instance that lacks common object methods like pick and omit.
🌐
StudyRaid
app.studyraid.com › en › read › 11289 › 352192 › merging-multiple-schemas
Understand merging multiple schemas
January 5, 2025 - Schema merging allows you to combine multiple Zod schemas into a single, unified schema. The most common methods for merging schemas are .merge(), .extend(), and .intersection().
Find elsewhere
🌐
Codefinity
codefinity.com › courses › v2 › 4c3ce27c-96a7-4a06-9d0f-fe398d005ef0 › 64929d56-59a2-46ef-b840-4b42533f8261 › 222444aa-bf3b-42d4-ad36-1bd138cdd827
Learn Composing and Reusing Schemas | Advanced Validation with Zod
When building complex forms, you often need to validate data that shares structure with other parts of your application. Instead of rewriting validation logic for each form, you can compose and reuse Zod schemas using the .merge() and .extend() methods.
🌐
GitHub
github.com › colinhacks › zod › issues › 3745
How to union or merge two or more z.record schemas · Issue #3745 · colinhacks/zod
September 6, 2024 - import { z } from 'zod'; // Define the schemas for the individual records const record1Schema = z.record(z.enum(['a', 'b']), z.union([z.number(), z.string()])); const record2Schema = z.record(z.enum(['c', 'd']), z.union([z.boolean(), z.string()])); // Define the merged schema const mergedSchema = z.union([record1Schema, record2Schema]); // Example usage const example1 = { a: 42, b: 'hello' }; // Valid according to record1Schema const example2 = { c: true, d: 'world' }; // Valid according to record2Schema const example3 = { a: 42, c: true }; // Valid according to mergedSchema console.log(record1Schema.safeParse(example1)); // Success console.log(record2Schema.safeParse(example2)); // Success console.log(mergedSchema.safeParse(example3)); // Error ·
Author   colinhacks
🌐
Zod
zod.dev › v4 › changelog
Migration guide | Zod
const mySchema = z.object({ a: z.any(), b: z.unknown() }); // Zod 3: { a?: any; b?: unknown }; // Zod 4: { a: any; b: unknown }; The .merge() method on ZodObject has been deprecated in favor of .extend().
🌐
Answer Overflow
answeroverflow.com › m › 1251907157083947090
How do I merge two Zod schemas? - Theo's Typesafe Cult
June 16, 2024 - Hey guys, I am creating a form with react-hook-form and Zod. The form was too long, so I decided to divide it into multi step form, with each step having separate Zod schema. I am storing the form data locally until all steps are complete. Now, I want a Zod schema for combining the schemas ...
🌐
Total TypeScript
totaltypescript.com › tutorials › zod › zod-section › composing-objects › solution
Refactoring Duplicated Schemas | Total TypeScript
June 7, 2023 - Merge, what it means, is you take the base ObjectWithId and you merge it with another object. You can see that this is slightly more verbose than the other version, then just extend, because here we're we get to pass in an object containing ...
🌐
GitHub
github.com › colinhacks › zod › discussions › 592
Merging onto union · colinhacks/zod · Discussion #592
One pretty basic problem is that a union might not be a union of objects where merge or extend are possible. Consider this: const propSchema = z.union([z.boolean(), z.null()]); The type of propSchema is a ZodUnion which doesn't have a merge method since ZodBoolean and ZodNull aren't "mergeable".
Author   colinhacks
🌐
GitHub
v3.zod.dev
.merge
TypeScript-first schema validation with static type inference
🌐
GitHub
github.com › colinhacks › zod › discussions › 2984
Calling `.merge` or `.extend` on recursive types · colinhacks/zod · Discussion #2984
const _HashSpecBase = z.object({ type: z.ZodLiteral('object') }); type _HashSpecType = z.infer<typeof _HashSpecBase> & { value_type?: string | z.infer<typeof ArraySpec>; }; const HashSpec: z.ZodType<_HashSpecType> = _HashSpecBase.merge( z.object({ value_type: z.lazy(() => z.union([z.string(), ArraySpec]).optional()) }) ); const _ArraySpecBase = z.object({ type: z.ZodLiteral('array') }); type _ArraySpecType = z.infer<typeof _ArraySpecBase> & { contains?: string | number | z.infer<typeof HashSpec>; }; const ArraySpec: z.ZodType<_ArraySpecType> = _ArraySpecBase.merge( z.object({ contains: z.lazy(() => z.union([z.string(), z.number(), HashSpec]).optional()) }) );
Author   colinhacks
🌐
Stack Overflow
stackoverflow.com › questions › 76805395 › typescript-merging-two-zod-schemas-where-one-is-a-generic-schema-how-do-i-sor
Typescript - Merging two Zod schemas where one is a generic schema, how do I sort out this type error? - Stack Overflow
Input: takes in a generic Zod schema and an object that abides by that schema (originalSchema and item). Process: adds a property to the schema (in this case version: z.number()). It also adds the property/value to the object (version: 1) Output: The merged schema, and an item that abides by the merged schema (mergedSchema and itemWithVersioning)
🌐
GitHub
github.com › colinhacks › zod › discussions › 2703
What's the use case for intersect over merge? · colinhacks/zod · Discussion #2703
September 2, 2023 - Though in many cases, it is recommended to use A.merge(B) to merge two objects. The .merge method returns a new ZodObject instance, whereas A.and(B) returns a less useful ZodIntersection instance that lacks common object methods like pick and omit.
Author   colinhacks
🌐
LogRocket
blog.logrocket.com › home › schema validation in typescript with zod
Schema validation in TypeScript with Zod - LogRocket Blog
July 30, 2024 - Certain Zod methods can help you compose and manage complex scheme objects effectively. We’ll cover this in three phases: nesting small schemas to form a big yet manageable schema object, deriving a new schema from an existing one, and then merging different schemas to create a new one.