🎥 Make videos programmatically with React
`remotion`, `@remotion/effects`, `@remotion/starburst`: Validate mandatory effect params
JonnyBurger585ee61ab854
diff --git a/packages/core/src/effects/create-effect.ts b/packages/core/src/effects/create-effect.tsindex 8d12d90846..b1ea24dee4 100644--- a/packages/core/src/effects/create-effect.ts+++ b/packages/core/src/effects/create-effect.ts@@ -32,6 +32,7 @@ export const createEffect = <P, S>(// memoization key. Without this, toggling `disabled` via code/drag overrides// would not invalidate the cached `EffectDefinitionAndStack`.const userCalculateKey = definition.calculateKey;+ const validateParams = definition.validateParams;const widened: EffectDefinition<unknown, unknown> = {...(definition as unknown as EffectDefinition<unknown, unknown>),calculateKey: (params: unknown) => {@@ -47,11 +48,14 @@ export const createEffect = <P, S>(params: P & {readonly disabled?: boolean} = {} as P & {readonly disabled?: boolean;},- ): EffectDescriptor<unknown> => ({- definition: widened,- params,- effectKey: widened.calculateKey(params),- memoized: false,- });+ ): EffectDescriptor<unknown> => {+ validateParams?.(params as P);+ return {+ definition: widened,+ params,+ effectKey: widened.calculateKey(params),+ memoized: false,+ };+ };return factory;};diff --git a/packages/core/src/effects/effect-types.ts b/packages/core/src/effects/effect-types.tsindex 6ddc4bb15c..178cd422bc 100644--- a/packages/core/src/effects/effect-types.ts+++ b/packages/core/src/effects/effect-types.ts@@ -43,6 +43,8 @@ export type EffectDefinition<P, S = unknown> = {readonly apply: (params: EffectApplyParams<P, S>) => void;readonly cleanup: (state: S) => void;readonly schema: SequenceSchema;+ /** Throws when mandatory params are missing or invalid. Called by `createEffect` before returning a descriptor. */+ readonly validateParams?: (params: P) => void;};type BaseEffectDescriptor<P = unknown> = {diff --git a/packages/core/src/test/create-effect-validate-params.test.ts b/packages/core/src/test/create-effect-validate-params.test.tsnew file mode 100644index 0000000000..a60772bf2a--- /dev/null+++ b/packages/core/src/test/create-effect-validate-params.test.ts@@ -0,0 +1,35 @@+import {expect, test} from 'bun:test';+import {createEffect} from '../effects/create-effect.js';++type RequiredParams = {+ readonly value: number;+};++const makeEffectWithValidation = () =>+ createEffect<RequiredParams, null>({+ type: 'test/required',+ label: 'Required',+ backend: '2d',+ calculateKey: (p) => `required-${p.value}`,+ setup: () => null,+ apply: () => undefined,+ cleanup: () => undefined,+ schema: {+ value: {type: 'number', default: undefined, description: 'Value'},+ },+ validateParams: (params) => {+ if (typeof params.value !== 'number' || !Number.isFinite(params.value)) {+ throw new TypeError(+ `"value" must be a finite number, but got ${JSON.stringify(params.value)}`,+ );+ }+ },+ });++test('createEffect calls validateParams when the factory is invoked', () => {+ const effect = makeEffectWithValidation();+ expect(() => effect({} as RequiredParams)).toThrow(+ '"value" must be a finite number, but got undefined',+ );+ expect(() => effect({value: 1})).not.toThrow();+});diff --git a/packages/effects/package.json b/packages/effects/package.jsonindex 1b636d742d..3ae7381e85 100644--- a/packages/effects/package.json+++ b/packages/effects/package.json@@ -10,6 +10,7 @@},"type": "module","scripts": {+ "test": "bun test src/test","formatting": "oxfmt src --check","format": "oxfmt src","lint": "eslint src",diff --git a/packages/effects/src/blur/index.ts b/packages/effects/src/blur/index.tsindex 2ddbc62d77..9e7c984226 100644--- a/packages/effects/src/blur/index.ts+++ b/packages/effects/src/blur/index.ts@@ -1,5 +1,9 @@import type {SequenceSchema} from 'remotion';import {Internals} from 'remotion';+import {+ assertEffectParamsObject,+ assertRequiredFiniteNumber,+} from '../validate-effect-param.js';const {createEffect} = Internals;import {@@ -19,11 +23,16 @@ const blurSchema = {min: 0,max: 100,step: 1,- default: 10,+ default: undefined,description: 'Blur radius',},} as const satisfies SequenceSchema;+const validateBlurParams = (params: BlurParams): void => {+ assertEffectParamsObject(params, 'Blur');+ assertRequiredFiniteNumber(params.radius, 'radius');+};+export const blur = createEffect<BlurParams, BlurState>({type: 'remotion/blur',label: 'Blur',@@ -41,4 +50,5 @@ export const blur = createEffect<BlurParams, BlurState>({},cleanup: (state) => cleanupBlur(state),schema: blurSchema,+ validateParams: validateBlurParams,});diff --git a/packages/effects/src/test/effect-params.test.ts b/packages/effects/src/test/effect-params.test.tsnew file mode 100644index 0000000000..a9ae748b2f--- /dev/null+++ b/packages/effects/src/test/effect-params.test.ts@@ -0,0 +1,23 @@+import {expect, test} from 'bun:test';+import {blur} from '../blur/index.js';+import {tint} from '../tint.js';++test('tint() throws when color is not passed', () => {+ expect(() => tint({} as Parameters<typeof tint>[0])).toThrow(+ '"color" must be a non-empty string, but got undefined',+ );+});++test('tint() accepts valid params', () => {+ expect(() => tint({color: '#ff0000'})).not.toThrow();+});++test('blur() throws when radius is not passed', () => {+ expect(() => blur({} as Parameters<typeof blur>[0])).toThrow(+ '"radius" must be a finite number, but got undefined',+ );+});++test('blur() accepts valid params', () => {+ expect(() => blur({radius: 4})).not.toThrow();+});diff --git a/packages/effects/src/tint.ts b/packages/effects/src/tint.tsindex eb8caeb21a..e750e6348e 100644--- a/packages/effects/src/tint.ts+++ b/packages/effects/src/tint.ts@@ -1,15 +1,18 @@import type {SequenceSchema} from 'remotion';import {Internals} from 'remotion';+import {+ assertEffectParamsObject,+ assertRequiredColor,+} from './validate-effect-param.js';const {createEffect} = Internals;const DEFAULT_AMOUNT = 0.5 as const;-const DEFAULT_COLOR = '#ff0000' as const;export const tintSchema = {color: {type: 'color',- default: DEFAULT_COLOR,+ default: '#ff0000',description: 'Color',},amount: {@@ -37,6 +40,11 @@ const resolve = (p: TintParams): TintResolved => ({amount: p.amount ?? DEFAULT_AMOUNT,});+const validateTintParams = (params: TintParams): void => {+ assertEffectParamsObject(params, 'Tint');+ assertRequiredColor(params.color, 'color');+};+// Tints the source with a flat color. `amount` controls the blend strength// (0 = no tint, 1 = full color over opaque pixels). Operates on the 2D// backend; tinting respects the source's alpha mask.@@ -77,4 +85,5 @@ export const tint = createEffect<TintParams, null>({},cleanup: () => undefined,schema: tintSchema,+ validateParams: validateTintParams,});diff --git a/packages/effects/src/validate-effect-param.ts b/packages/effects/src/validate-effect-param.tsnew file mode 100644index 0000000000..ef21742908--- /dev/null+++ b/packages/effects/src/validate-effect-param.ts@@ -0,0 +1,29 @@+export const assertEffectParamsObject = (+ params: unknown,+ effectLabel: string,+): void => {+ if (params === null || typeof params !== 'object') {+ throw new TypeError(+ `${effectLabel} effect requires a parameters object, but got ${JSON.stringify(params)}`,+ );+ }+};++export const assertRequiredFiniteNumber = (+ value: unknown,+ name: string,+): void => {+ if (typeof value !== 'number' || !Number.isFinite(value)) {+ throw new TypeError(+ `"${name}" must be a finite number, but got ${JSON.stringify(value)}`,+ );+ }+};++export const assertRequiredColor = (value: unknown, name: string): void => {+ if (typeof value !== 'string' || value.length === 0) {+ throw new TypeError(+ `"${name}" must be a non-empty string, but got ${JSON.stringify(value)}`,+ );+ }+};diff --git a/packages/starburst/package.json b/packages/starburst/package.jsonindex b8d8c09052..0d2cea49ea 100644--- a/packages/starburst/package.json+++ b/packages/starburst/package.json@@ -10,6 +10,7 @@},"type": "module","scripts": {+ "test": "bun test src/test","formatting": "oxfmt src --check","format": "oxfmt src","lint": "eslint src",diff --git a/packages/starburst/src/starburst-effect.ts b/packages/starburst/src/starburst-effect.tsindex 734d8ab5bf..73301c079d 100644--- a/packages/starburst/src/starburst-effect.ts+++ b/packages/starburst/src/starburst-effect.ts@@ -1,4 +1,4 @@-import type {EffectDescriptor, SequenceSchema} from 'remotion';+import type {SequenceSchema} from 'remotion';import {Internals} from 'remotion';import {hexToRgb} from './hex-to-rgb';@@ -10,7 +10,7 @@ export const starburstEffectSchema = {min: 2,max: 100,step: 1,- default: 12,+ default: undefined,description: 'Number of Rays',},rotation: {@@ -86,6 +86,12 @@ const resolve = (p: StarburstEffectParams): StarburstResolved => ({});const validateStarburstEffectParams = (params: StarburstEffectParams): void => {+ if (params === null || typeof params !== 'object') {+ throw new TypeError(+ `Starburst effect requires a parameters object, but got ${JSON.stringify(params)}`,+ );+ }+const {rays, colors} = params;if (typeof rays !== 'number' || !Number.isFinite(rays)) {@@ -291,7 +297,7 @@ const linkProgram = (return program;};-const starburstImpl = createEffect<StarburstEffectParams, StarburstGlState>({+export const starburst = createEffect<StarburstEffectParams, StarburstGlState>({type: 'remotion/starburst',label: 'Starburst',backend: 'webgl2',@@ -489,14 +495,5 @@ const starburstImpl = createEffect<StarburstEffectParams, StarburstGlState>({gl.deleteTexture(paletteTexture);},schema: starburstEffectSchema,+ validateParams: validateStarburstEffectParams,});--// WebGL2 effect: composites the same radial color rays as `<Starburst>` over the-// incoming canvas using premultiplied alpha-over. Experimental; use via-// `StarburstInternals.starburst(...)`.-export const starburst = (- params: StarburstEffectParams,-): EffectDescriptor<unknown> => {- validateStarburstEffectParams(params);- return starburstImpl(params);-};diff --git a/packages/starburst/src/test/starburst-effect-params.test.ts b/packages/starburst/src/test/starburst-effect-params.test.tsnew file mode 100644index 0000000000..7caeca8e29--- /dev/null+++ b/packages/starburst/src/test/starburst-effect-params.test.ts@@ -0,0 +1,16 @@+import {expect, test} from 'bun:test';+import {starburst} from '../starburst-effect.js';++test('starburst() throws when rays is not passed', () => {+ expect(() =>+ starburst({+ colors: ['#ff0000', '#00ff00'],+ } as unknown as Parameters<typeof starburst>[0]),+ ).toThrow('"rays" must be a finite number, but got undefined');+});++test('starburst() accepts valid params', () => {+ expect(() =>+ starburst({rays: 12, colors: ['#ff0000', '#00ff00']}),+ ).not.toThrow();+});