Skip to content

Commit d1002d1

Browse files
fix: ensure phrase is a string (COR-10013) (#593)
### TL;DR Fix variable replacement to handle non-string inputs by converting them to strings. ### What changed? Modified the `replaceVariables` function to explicitly convert non-string inputs to strings before processing. Previously, the function would attempt to trim potentially non-string values, which was rarely but occasionally resulting in a `phrase?.trim is not a function` error. Now, the function safely converts the input to a string first, handling all type issues. ### How to test? 1. Call `replaceVariables` with different types of inputs: - String values - Numbers - Undefined values - Null values - Objects 2. Verify that the function correctly converts all inputs to strings without errors 3. Ensure that variable replacement still works as expected with the converted strings ### Why make this change? This change improves the robustness of the `replaceVariables` function by ensuring it can safely handle any type of input. Previously, calling methods like `trim()` on non-string values could cause runtime errors. This fix ensures consistent behavior regardless of input type, preventing potential crashes when the function receives unexpected data types. Co-authored-by: Amanda <amanda.steinhauer@voiceflow.com>
1 parent f36349e commit d1002d1

1 file changed

Lines changed: 2 additions & 1 deletion

File tree

packages/common/src/utils/variables.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@ export function replaceVariables(
8484
keepTypeIfOnlyVariable = false,
8585
}: { modifier?: ((variable: unknown) => unknown) | undefined; trim?: boolean; keepTypeIfOnlyVariable?: boolean } = {}
8686
): string | unknown {
87-
const formattedPhrase = trim ? phrase?.trim() : phrase;
87+
const stringPhrase = typeof phrase === 'string' ? phrase : String(phrase ?? '');
88+
const formattedPhrase = trim ? stringPhrase.trim() : phrase;
8889

8990
if (!formattedPhrase) {
9091
return '';

0 commit comments

Comments
 (0)