When to Use
When validating objects that share a base shape but have type-specific required/optional fields — distinguished by a discriminator field (liketype, kind, or variant).
Pattern
Why Not z.union()?
z.discriminatedUnion("type", [...]) is faster — it reads the discriminator field first and only validates the matching branch. z.union([...]) tries every branch sequentially and produces confusing error messages.
Gotcha: .extend() Strips Unknown Keys
Zod schemas with.extend() use .strip() mode by default — unknown keys are silently removed from the output. If you need to detect forbidden keys (like “composition must not appear on fragments”), check the raw input:
Gotcha: .default() Injects Values
gate: boolean (not gate?: boolean).