Skip to content

🎥 Make videos programmatically with React

validate composition and sequence props

Jonny Burgerc389b8f2a27a
diff --git a/packages/core/src/Composition.tsx b/packages/core/src/Composition.tsx
index 3aa9082bed..5ca52c33d6 100644
--- a/packages/core/src/Composition.tsx
+++ b/packages/core/src/Composition.tsx
@@ -58,6 +58,46 @@ export const Composition = <T,>({
`Composition id can only contain a-z, A-Z, 0-9 and -. You passed ${id}`
);
}
+ if (typeof width !== 'number') {
+ throw new Error(
+ `The "width" of a composition must be a number, but you passed a ${typeof width}`
+ );
+ }
+ if (width <= 0) {
+ throw new TypeError(
+ `The "width" of a composition must be positive, but got ${width}.`
+ );
+ }
+ if (typeof height !== 'number') {
+ throw new Error(
+ `The "height" of a composition must be a number, but you passed a ${typeof height}`
+ );
+ }
+ if (height <= 0) {
+ throw new TypeError(
+ `The "height" of a composition must be positive, but got ${height}.`
+ );
+ }
+ if (typeof durationInFrames !== 'number') {
+ throw new Error(
+ `The "durationInFrames" of a composition must be a number, but you passed a ${typeof durationInFrames}`
+ );
+ }
+ if (durationInFrames <= 0) {
+ throw new TypeError(
+ `The "durationInFrames" of a composition must be positive, but got ${durationInFrames}.`
+ );
+ }
+ if (typeof fps !== 'number') {
+ throw new Error(
+ `The "fps" of a composition must be a number, but you passed a ${typeof fps}`
+ );
+ }
+ if (fps <= 0) {
+ throw new TypeError(
+ `The "fps" of a composition must be positive, but got ${fps}.`
+ );
+ }
registerComposition<T>({
durationInFrames,
fps,
diff --git a/packages/core/src/get-timeline-clip-name.ts b/packages/core/src/get-timeline-clip-name.ts
index 28d07283fa..5bbac161f3 100644
--- a/packages/core/src/get-timeline-clip-name.ts
+++ b/packages/core/src/get-timeline-clip-name.ts
@@ -4,7 +4,7 @@ export const getTimelineClipName = (children: any, i = 0): string => {
// Must be name, not ID
const name = ch?.type?.name;
const root = name ? ((' '.repeat(i) + name) as string) : null;
- if (ch.props.children) {
+ if (ch?.props.children) {
const chName = getTimelineClipName(children.props.children, i + 1);
return [root, chName].filter(Boolean).join('\n');
}
diff --git a/packages/core/src/sequencing/index.tsx b/packages/core/src/sequencing/index.tsx
index e65305e640..8541058e71 100644
--- a/packages/core/src/sequencing/index.tsx
+++ b/packages/core/src/sequencing/index.tsx
@@ -18,6 +18,7 @@ type SequenceContextType = {
export const SequenceContext = createContext<SequenceContextType | null>(null);
export const Sequence: React.FC<{
+ children: React.ReactNode;
from: number;
durationInFrames: number;
name?: string;
@@ -35,6 +36,22 @@ export const Sequence: React.FC<{
);
}
+ if (typeof durationInFrames !== 'number') {
+ throw new TypeError(
+ `You passed to durationInFrames an argument of type ${typeof durationInFrames}, but it must be a number.`
+ );
+ }
+ if (durationInFrames <= 0) {
+ throw new TypeError(
+ `durationInFrames must be positive, but got ${durationInFrames}`
+ );
+ }
+ if (typeof from !== 'number') {
+ throw new TypeError(
+ `You passed to the "from" props of your <Sequence> an argument of type ${typeof from}, but it must be a number.`
+ );
+ }
+
const contextValue = useMemo((): SequenceContextType => {
return {
from: actualFrom,
diff --git a/packages/core/src/test/composition-validation.test.tsx b/packages/core/src/test/composition-validation.test.tsx
new file mode 100644
index 0000000000..3c57e96f54
--- /dev/null
+++ b/packages/core/src/test/composition-validation.test.tsx
@@ -0,0 +1,46 @@
+import {render} from '@testing-library/react';
+import React from 'react';
+import {Composition} from '..';
+import {RemotionRoot} from '../RemotionRoot';
+import {expectToThrow} from './expect-to-throw';
+
+const AnyComp: React.FC = () => null;
+
+test('It should throw if multiple components have the same id', () => {
+ expectToThrow(
+ () =>
+ render(
+ <RemotionRoot>
+ {/**
+ // @ts-expect-error*/}
+ <Composition
+ lazyComponent={() => Promise.resolve({default: AnyComp})}
+ durationInFrames={100}
+ fps={30}
+ height={100}
+ id="id"
+ />
+ </RemotionRoot>
+ ),
+ /The "width/
+ );
+});
+
+test('It should throw if multiple components have the same id', () => {
+ expectToThrow(
+ () =>
+ render(
+ <RemotionRoot>
+ <Composition
+ lazyComponent={() => Promise.resolve({default: AnyComp})}
+ durationInFrames={100}
+ fps={30}
+ height={-100}
+ width={100}
+ id="id"
+ />
+ </RemotionRoot>
+ ),
+ /The "height" of a composition must be positive, but got -100./
+ );
+});
diff --git a/packages/core/src/test/sequence-validation.test.tsx b/packages/core/src/test/sequence-validation.test.tsx
new file mode 100644
index 0000000000..40dc4871c3
--- /dev/null
+++ b/packages/core/src/test/sequence-validation.test.tsx
@@ -0,0 +1,30 @@
+import {render} from '@testing-library/react';
+import React from 'react';
+import {Sequence} from '../sequencing';
+import {expectToThrow} from './expect-to-throw';
+
+test('It should throw if Sequence has missing duration', () => {
+ expectToThrow(
+ // @ts-expect-error
+ () => render(<Sequence from={0} />),
+ /You passed to durationInFrames an argument of type undefined, but it must be a number./
+ );
+});
+
+test('It should allow null as children', () => {
+ expect(() =>
+ render(
+ <Sequence durationInFrames={100} from={0}>
+ {null}
+ </Sequence>
+ )
+ ).not.toThrow();
+});
+
+test('It should not allow no children at all', () => {
+ expectToThrow(
+ // @ts-expect-error
+ () => render(<Sequence from={0} />),
+ /You passed to durationInFrames an argument of type undefined, but it must be a number./
+ );
+});