Skip to content

🎥 Make videos programmatically with React

Remotion Studio: Fix crash when adding another discriminated union

Jonny Burger16b4619b62c6
diff --git a/packages/example/src/Root.tsx b/packages/example/src/Root.tsx
index 1b91a59cf9..9e8109d32d 100644
--- a/packages/example/src/Root.tsx
+++ b/packages/example/src/Root.tsx
@@ -1027,6 +1027,10 @@ export const Index: React.FC = () => {
list: [{name: 'first', age: 12}],
description: 'Sample description \nOn multiple lines',
dropdown: 'a' as const,
+ superSchema: [
+ {type: 'a' as const, a: {a: 'hi'}},
+ {type: 'b' as const, b: {b: 'hi'}},
+ ],
}}
/>
{/**
diff --git a/packages/example/src/SchemaTest/index.tsx b/packages/example/src/SchemaTest/index.tsx
index a61de4bc0b..c2795c9fe7 100644
--- a/packages/example/src/SchemaTest/index.tsx
+++ b/packages/example/src/SchemaTest/index.tsx
@@ -10,6 +10,18 @@ export const schemaTestSchema = z.object({
list: z.array(z.object({name: z.string(), age: z.number()})),
description: zTextarea().nullable(),
dropdown: z.enum(['a', 'b', 'c']),
+ superSchema: z.array(
+ z.discriminatedUnion('type', [
+ z.object({
+ type: z.literal('a'),
+ a: z.object({a: z.string()}),
+ }),
+ z.object({
+ type: z.literal('b'),
+ b: z.object({b: z.string()}),
+ }),
+ ]),
+ ),
});
export const schemaArrayTestSchema = z.array(z.number());
diff --git a/packages/studio/src/components/RenderModal/SchemaEditor/ZodArrayEditor.tsx b/packages/studio/src/components/RenderModal/SchemaEditor/ZodArrayEditor.tsx
index f43aa9a971..aa119fb21c 100644
--- a/packages/studio/src/components/RenderModal/SchemaEditor/ZodArrayEditor.tsx
+++ b/packages/studio/src/components/RenderModal/SchemaEditor/ZodArrayEditor.tsx
@@ -1,6 +1,10 @@
import React, {useMemo, useState} from 'react';
import type {z} from 'zod';
-import {useZodIfPossible} from '../../get-zod-if-possible';
+import {
+ useZodIfPossible,
+ useZodTypesIfPossible,
+} from '../../get-zod-if-possible';
+import {createZodValues} from './create-zod-values';
import {deepEqual} from './deep-equal';
import {Fieldset} from './Fieldset';
import {useLocalState} from './local-state';
@@ -56,6 +60,8 @@ export const ZodArrayEditor: React.FC<{
throw new Error('expected zod');
}
+ const zodTypes = useZodTypesIfPossible();
+
const typeName = def.typeName as z.ZodFirstPartyTypeKind;
if (typeName !== z.ZodFirstPartyTypeKind.ZodArray) {
throw new Error('expected object');
@@ -103,7 +109,9 @@ export const ZodArrayEditor: React.FC<{
def={def}
index={i}
jsonPath={jsonPath}
- defaultValue={defaultValue[i]}
+ defaultValue={
+ defaultValue[i] ?? createZodValues(def.type, z, zodTypes)
+ }
onSave={onSave}
showSaveButton={showSaveButton}
saving={saving}