Skip to content

🎥 Make videos programmatically with React

Support shared enum schema keys (#9038)

Jonny Burgerca4903493f6f
diff --git a/packages/core/src/flatten-schema.ts b/packages/core/src/flatten-schema.ts
index ed5a237f78..1f0371e732 100644
--- a/packages/core/src/flatten-schema.ts
+++ b/packages/core/src/flatten-schema.ts
@@ -33,9 +33,7 @@ export const getFlatSchemaWithAllKeys = (
const addKey = (key: string, field: InteractivitySchema[string]) => {
if (key in out) {
- throw new Error(
- `Duplicate key "${key}" in schema: discriminated union variants must not share keys`,
- );
+ return;
}
out[key] = field;
diff --git a/packages/core/src/test/with-interactivity-schema-helpers.test.ts b/packages/core/src/test/with-interactivity-schema-helpers.test.ts
index 5da0385c89..08366e750d 100644
--- a/packages/core/src/test/with-interactivity-schema-helpers.test.ts
+++ b/packages/core/src/test/with-interactivity-schema-helpers.test.ts
@@ -1,6 +1,9 @@
import {expect, test} from 'bun:test';
import {solidSchema} from '../effects/Solid.js';
-import {getFlatSchemaWithAllKeys} from '../flatten-schema.js';
+import {
+ flattenActiveSchema,
+ getFlatSchemaWithAllKeys,
+} from '../flatten-schema.js';
import {htmlInCanvasSchema} from '../HtmlInCanvas.js';
import {Interactive} from '../Interactive.js';
import {
@@ -83,6 +86,72 @@ test('getFlatSchema(sequenceSchema) exposes every variant key', () => {
);
});
+test('getFlatSchema allows enum variants to share prop keys', () => {
+ const schema = {
+ type: {
+ type: 'enum',
+ default: 'underline',
+ description: 'Type',
+ variants: {
+ underline: {
+ color: {
+ type: 'color',
+ default: '#111111',
+ description: 'Underline color',
+ },
+ },
+ highlight: {
+ color: {
+ type: 'color',
+ default: '#eeee00',
+ description: 'Highlight color',
+ },
+ },
+ bracket: {},
+ },
+ },
+ } as const;
+
+ const flat = getFlatSchemaWithAllKeys(schema);
+ expect(Object.keys(flat).sort()).toEqual(['color', 'type']);
+ expect(flat.color.type).toBe('color');
+});
+
+test('flattenActiveSchema resolves shared keys from the selected enum variant', () => {
+ const schema = {
+ type: {
+ type: 'enum',
+ default: 'underline',
+ description: 'Type',
+ variants: {
+ underline: {
+ color: {
+ type: 'color',
+ default: '#111111',
+ description: 'Underline color',
+ },
+ },
+ highlight: {
+ color: {
+ type: 'color',
+ default: '#eeee00',
+ description: 'Highlight color',
+ },
+ },
+ },
+ },
+ } as const;
+
+ const active = flattenActiveSchema(schema, (key) =>
+ key === 'type' ? 'highlight' : undefined,
+ );
+ expect(active.color).toEqual({
+ type: 'color',
+ default: '#eeee00',
+ description: 'Highlight color',
+ });
+});
+
test('extendSchemaWithSequenceName adds hidden name to wrapped schemas', () => {
const flat = getFlatSchemaWithAllKeys(
extendSchemaWithSequenceName({
diff --git a/packages/studio-server/src/test/get-all-schema-keys.test.ts b/packages/studio-server/src/test/get-all-schema-keys.test.ts
index 1a63c9dc81..a3a8a73fe5 100644
--- a/packages/studio-server/src/test/get-all-schema-keys.test.ts
+++ b/packages/studio-server/src/test/get-all-schema-keys.test.ts
@@ -31,8 +31,8 @@ test('getAllSchemaKeys returns every key across all enum variants', () => {
);
});
-test('getFlatSchema throws when discriminated union variants share a key', () => {
- const conflictingSchema: InteractivitySchema = {
+test('getFlatSchema dedupes keys shared by discriminated union variants', () => {
+ const schema: InteractivitySchema = {
mode: {
type: 'enum',
default: 'a',
@@ -56,7 +56,8 @@ test('getFlatSchema throws when discriminated union variants share a key', () =>
},
};
- expect(() => getFlatSchemaWithAllKeys(conflictingSchema)).toThrow(
- 'Duplicate key "shared"',
- );
+ expect(Object.keys(getFlatSchemaWithAllKeys(schema)).sort()).toEqual([
+ 'mode',
+ 'shared',
+ ]);
});