Skip to content

Feature: Accept z.discriminatedUnion (and other unions) as composition schema root #6976

@JonnyBurger

Description

@JonnyBurger

Summary

Remotion Studio currently rejects any composition schema whose top-level Zod type is not a plain z.object(). Using z.discriminatedUnion() (a common pattern for preset-specific props) surfaces:

The top-level type of the schema must be a pure z.object. Instead got a schema of type discriminatedUnion

This forces a flat z.object() with all fields optional/defaulted and manual .superRefine() or runtime branching, which is weaker for typing and clutters the Studio props UI with irrelevant fields.

Motivation

z.discriminatedUnion("preset", [...]) is idiomatic Zod for “one of several shapes” (e.g. caption presets where only some variants have outline, centerStageFont, etc.). TypeScript narrows correctly on preset, and each branch can omit unrelated keys entirely.

Supporting this at the root would align Remotion with common Zod patterns and avoid workarounds.

Minimal example

import { z } from "zod";

const CaptionsSchema = z.discriminatedUnion("preset", [
  z.object({
    preset: z.literal("Simple"),
    track: z.string(),
    fontSize: z.number().default(48),
  }),
  z.object({
    preset: z.literal("Fancy"),
    track: z.string(),
    fontSize: z.number().default(48),
    outline: z.boolean().default(false), // only meaningful for Fancy
  }),
]);

// <Composition schema={CaptionsSchema} ... />
// Today: Studio shows the error above.
// Desired: Schema editor / validation accept discriminated unions.

Possible directions

  • Treat ZodDiscriminatedUnion like z.object for the editor when the discriminator is a known literal field (e.g. preset).
  • Or document a supported wrapper if full editor support is hard (e.g. codegen from union → flat object for Studio only).

Workaround

Use a single z.object({ preset, ...allFields }) with defaults and ignore irrelevant fields at runtime, or validate with .superRefine().


Happy to help test or sketch editor behavior for a fixed set of union members if useful.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions