Skip to content

🎥 Make videos programmatically with React

better validation in renderFrames()

Jonny Burger84e232e45450
diff --git a/packages/core/src/Composition.tsx b/packages/core/src/Composition.tsx
index e6adb6aa92..dc61084201 100644
--- a/packages/core/src/Composition.tsx
+++ b/packages/core/src/Composition.tsx
@@ -57,13 +57,13 @@ export const Composition = <T,>({
);
}
- validateDimension(width, 'width', 'Composition');
- validateDimension(height, 'height', 'Composition');
+ validateDimension(width, 'width', 'of the <Composition/> component');
+ validateDimension(height, 'height', 'of the <Composition/> component');
validateDurationInFrames(
durationInFrames,
'of the <Composition/> component'
);
- validateFps(fps);
+ validateFps(fps, 'as a prop of the <Composition/> component');
registerComposition<T>({
durationInFrames,
fps,
diff --git a/packages/core/src/spring/measure-spring.ts b/packages/core/src/spring/measure-spring.ts
index 2f56133a62..a8377ecf93 100644
--- a/packages/core/src/spring/measure-spring.ts
+++ b/packages/core/src/spring/measure-spring.ts
@@ -40,7 +40,7 @@ export function measureSpring({
throw new TypeError('Threshold is below 0');
}
- validateFps(fps);
+ validateFps(fps, 'to the measureSpring() function');
const range = Math.abs(from - to);
let frame = 0;
diff --git a/packages/core/src/validation/validate-dimensions.ts b/packages/core/src/validation/validate-dimensions.ts
index 636aee2f78..a81ccf932d 100644
--- a/packages/core/src/validation/validate-dimensions.ts
+++ b/packages/core/src/validation/validate-dimensions.ts
@@ -1,35 +1,35 @@
export const validateDimension = (
amount: number,
nameOfProp: string,
- componentName: string
+ location: string
) => {
if (typeof amount !== 'number') {
throw new Error(
- `The "${nameOfProp}" prop of the <${componentName}/> component must be a number, but you passed a value of type ${typeof amount}`
+ `The "${nameOfProp}" prop ${location} must be a number, but you passed a value of type ${typeof amount}`
);
}
if (isNaN(amount)) {
throw new TypeError(
- `The "${nameOfProp}" prop of the <${componentName}/> component must not be NaN, but is NaN.`
+ `The "${nameOfProp}" prop ${location} must not be NaN, but is NaN.`
);
}
if (!Number.isFinite(amount)) {
throw new TypeError(
- `The "${nameOfProp}" prop of the <${componentName}/> component must be finite, but is ${amount}.`
+ `The "${nameOfProp}" prop ${location} must be finite, but is ${amount}.`
);
}
if (amount % 1 !== 0) {
throw new TypeError(
- `The "${nameOfProp}" prop of the <${componentName}/> component must be an integer, but is ${amount}.`
+ `The "${nameOfProp}" prop ${location} must be an integer, but is ${amount}.`
);
}
if (amount <= 0) {
throw new TypeError(
- `The "${nameOfProp}" prop of the <${componentName}/> component must be positive, but got ${amount}.`
+ `The "${nameOfProp}" prop ${location} must be positive, but got ${amount}.`
);
}
};
diff --git a/packages/core/src/validation/validate-fps.ts b/packages/core/src/validation/validate-fps.ts
index 850907bfa9..38a443cd9d 100644
--- a/packages/core/src/validation/validate-fps.ts
+++ b/packages/core/src/validation/validate-fps.ts
@@ -1,19 +1,21 @@
-export const validateFps = (fps: number) => {
+export const validateFps = (fps: number, location: string) => {
if (typeof fps !== 'number') {
throw new Error(
- `"fps" must be a number, but you passed a value of type ${typeof fps}`
+ `"fps" must be a number, but you passed a value of type ${typeof fps} ${location}`
);
}
if (!Number.isFinite(fps)) {
- throw new Error(`"fps" must be a finite, but you passed ${fps}`);
+ throw new Error(
+ `"fps" must be a finite, but you passed ${fps} ${location}`
+ );
}
if (isNaN(fps)) {
- throw new Error(`"fps" must not be NaN, but got ${fps}`);
+ throw new Error(`"fps" must not be NaN, but got ${fps} ${location}`);
}
if (fps <= 0) {
- throw new TypeError(`"fps" must be positive, but got ${fps}.`);
+ throw new TypeError(`"fps" must be positive, but got ${fps} ${location}`);
}
};
diff --git a/packages/player/src/Player.tsx b/packages/player/src/Player.tsx
index 198a05925e..647d73d873 100644
--- a/packages/player/src/Player.tsx
+++ b/packages/player/src/Player.tsx
@@ -98,13 +98,21 @@ export const PlayerFn = <T,>(
);
}
- Internals.validateDimension(compositionHeight, 'compositionHeight', 'Player');
- Internals.validateDimension(compositionWidth, 'compositionWidth', 'Player');
+ Internals.validateDimension(
+ compositionHeight,
+ 'compositionHeight',
+ 'of the <Player /> component'
+ );
+ Internals.validateDimension(
+ compositionWidth,
+ 'compositionWidth',
+ 'of the <Player /> component'
+ );
Internals.validateDurationInFrames(
durationInFrames,
'of the <Player/> component'
);
- Internals.validateFps(fps);
+ Internals.validateFps(fps, 'as a prop of the <Player/> component');
if (typeof controls !== 'boolean' && typeof controls !== 'undefined') {
throw new TypeError(
diff --git a/packages/renderer/src/render.ts b/packages/renderer/src/render.ts
index e9307b3a08..803131f411 100644
--- a/packages/renderer/src/render.ts
+++ b/packages/renderer/src/render.ts
@@ -65,6 +65,24 @@ export const renderFrames = async ({
puppeteerInstance?: PuppeteerBrowser;
onError?: (info: OnErrorInfo) => void;
}): Promise<RenderFramesOutput> => {
+ Internals.validateDimension(
+ config.height,
+ 'height',
+ 'in the `config` object passed to `renderFrames()`'
+ );
+ Internals.validateDimension(
+ config.width,
+ 'width',
+ 'in the `config` object passed to `renderFrames()`'
+ );
+ Internals.validateFps(
+ config.fps,
+ 'in the `config` object of `renderFrames()`'
+ );
+ Internals.validateDurationInFrames(
+ config.durationInFrames,
+ 'in the `config` object passed to `renderFrames()`'
+ );
if (quality !== undefined && imageFormat !== 'jpeg') {
throw new Error(
"You can only pass the `quality` option if `imageFormat` is 'jpeg'."
diff --git a/packages/renderer/src/test/validate-config.test.ts b/packages/renderer/src/test/validate-config.test.ts
new file mode 100644
index 0000000000..0b40e84f1e
--- /dev/null
+++ b/packages/renderer/src/test/validate-config.test.ts
@@ -0,0 +1,64 @@
+import {renderFrames} from '../render';
+
+describe('Should validate invalid data passed to renderFrames', () => {
+ test('Invalid FPS', () => {
+ return expect(() =>
+ // @ts-expect-error
+ renderFrames({
+ config: {
+ durationInFrames: 100,
+ fps: -1,
+ height: 1000,
+ width: 1000,
+ },
+ })
+ ).rejects.toThrow(
+ /"fps" must be positive, but got -1 in the `config` object of `renderFrames\(\)`/
+ );
+ });
+ test('Invalid durationInFrames', async () => {
+ return expect(
+ // @ts-expect-error
+ renderFrames({
+ config: {
+ durationInFrames: 0.5,
+ fps: 30,
+ height: 1000,
+ width: 1000,
+ },
+ })
+ ).rejects.toThrow(
+ /The "durationInFrames" prop in the `config` object passed to `renderFrames\(\)` must be an integer, but got 0.5./
+ );
+ });
+ test('Invalid height', async () => {
+ return expect(
+ // @ts-expect-error
+ renderFrames({
+ config: {
+ durationInFrames: 1,
+ fps: 30,
+ height: 1000.5,
+ width: 1000,
+ },
+ })
+ ).rejects.toThrow(
+ /The "height" prop in the `config` object passed to `renderFrames\(\)` must be an integer, but is 1000.5./
+ );
+ });
+ test('Invalid width', async () => {
+ return expect(
+ // @ts-expect-error
+ renderFrames({
+ config: {
+ durationInFrames: 1,
+ fps: 30,
+ width: 1000.5,
+ height: 1000,
+ },
+ })
+ ).rejects.toThrow(
+ /The "width" prop in the `config` object passed to `renderFrames\(\)` must be an integer, but is 1000.5./
+ );
+ });
+});
diff --git a/packages/three/src/ThreeCanvas.tsx b/packages/three/src/ThreeCanvas.tsx
index e300ed54e0..187412e713 100644
--- a/packages/three/src/ThreeCanvas.tsx
+++ b/packages/three/src/ThreeCanvas.tsx
@@ -20,8 +20,16 @@ const Scale = ({ width, height }: { width: number; height: number }) => {
export const ThreeCanvas = (props: ThreeCanvasProps) => {
const { children, width, height, style, ...rest } = props;
- Internals.validateDimension(width, 'width', 'ThreeCanvas');
- Internals.validateDimension(height, 'height', 'ThreeCanvas');
+ Internals.validateDimension(
+ width,
+ 'width',
+ 'of the <ThreeCanvas /> component'
+ );
+ Internals.validateDimension(
+ height,
+ 'height',
+ 'of the <ThreeCanvas /> component'
+ );
const contexts = Internals.useRemotionContexts();
const actualStyle = {
width: props.width,