Skip to content

🎥 Make videos programmatically with React

`@remotion/fonts`: Add validation for loadFont() arguments

Jonny Burger6230afaa6b92
diff --git a/packages/fonts/src/get-font-format.ts b/packages/fonts/src/get-font-format.ts
index 230f4cd148..ccfb1b5069 100644
--- a/packages/fonts/src/get-font-format.ts
+++ b/packages/fonts/src/get-font-format.ts
@@ -1,6 +1,12 @@
export type FontFormat = 'woff2' | 'woff' | 'opentype' | 'truetype';
export const getFontFormat = (url: string): FontFormat => {
+ if (typeof url !== 'string') {
+ throw new TypeError(
+ `Expected a URL string but received ${url === undefined ? 'undefined' : typeof url}. Make sure to pass a "url" field in the options object of loadFont().`,
+ );
+ }
+
const ext = url.split('.').pop()?.toLowerCase();
switch (ext) {
case 'woff2':
diff --git a/packages/fonts/src/load-font.ts b/packages/fonts/src/load-font.ts
index edd3a8a675..eecd4f6c9c 100644
--- a/packages/fonts/src/load-font.ts
+++ b/packages/fonts/src/load-font.ts
@@ -18,21 +18,35 @@ export type LoadFontOptions = {
format?: FontFormat;
};
-export const loadFont = async ({
- family,
- url,
- ascentOverride,
- descentOverride,
- display,
- featureSettings,
- lineGapOverride,
- stretch,
- style,
- unicodeRange,
- weight,
- format,
- variant,
-}: LoadFontOptions): Promise<void> => {
+export const loadFont = async (options: LoadFontOptions): Promise<void> => {
+ if (typeof options !== 'object' || options === null) {
+ throw new TypeError(
+ `loadFont() requires an object as its argument, but received ${typeof options === 'string' ? `"${options}"` : typeof options}. If you want to load a Google Font, use the @remotion/google-fonts package instead. See: https://www.remotion.dev/docs/google-fonts/load-font`,
+ );
+ }
+
+ const {
+ family,
+ url,
+ ascentOverride,
+ descentOverride,
+ display,
+ featureSettings,
+ lineGapOverride,
+ stretch,
+ style,
+ unicodeRange,
+ weight,
+ format,
+ variant,
+ } = options;
+
+ if (typeof url !== 'string') {
+ throw new TypeError(
+ `loadFont() requires a "url" field in the options object, but received ${url === undefined ? 'undefined' : JSON.stringify(url)}. If you want to load a Google Font, use the @remotion/google-fonts package instead. See: https://www.remotion.dev/docs/google-fonts/load-font`,
+ );
+ }
+
const waitForFont = delayRender(
`Loading font ${family} (url: ${url}, format: ${format}, weight: ${weight}, style: ${style}, variant: ${variant}, ascentOverride: ${ascentOverride}, descentOverride: ${descentOverride}, display: ${display}, featureSettings: ${featureSettings}, lineGapOverride: ${lineGapOverride}, stretch: ${stretch}, unicodeRange: ${unicodeRange})`,
);