Skip to content

Commit aaea49e

Browse files
committed
fix: undefined template vars
1 parent bf4b7e3 commit aaea49e

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

libs/definitions/src/utils/field.utils.spec.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,5 +117,12 @@ describe('FieldUtils', () => {
117117
bar: 'BAR',
118118
})
119119
})
120+
121+
it('should replace undefined template variables with empty string', () => {
122+
const res = replaceTemplateFields(idsMap, { foo: 'Test {{ template.foo }}' }, {})
123+
expect(res).toEqual({
124+
foo: 'Test ',
125+
})
126+
})
120127
})
121128
})

libs/definitions/src/utils/field.utils.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,14 +106,17 @@ export function replaceTemplateFields(
106106
for (const match of matches) {
107107
// replace fields that only contain a template variable (the interpolation is removed)
108108
if (match[1].trim().startsWith('template.')) {
109-
newValue = newValue.replace(match[0], templateInputs[match[1].trim().replace('template.', '')])
109+
newValue = newValue.replace(match[0], templateInputs[match[1].trim().replace('template.', '')] ?? '')
110110
}
111111
// replace fields that contain template variables inside functions or other variables (the interpolation is kept)
112112
else {
113113
// replace "template." variables
114114
const templateRegex = /template\.([a-zA-Z0-9_\[\]\.]+)/g
115115
for (const templateMatch of Array.from((match[1].match(templateRegex) as RegExpMatchArray) ?? [])) {
116-
newValue = newValue.replace(templateMatch, templateInputs[templateMatch.trim().replace('template.', '')])
116+
newValue = newValue.replace(
117+
templateMatch,
118+
templateInputs[templateMatch.trim().replace('template.', '')] ?? '',
119+
)
117120
}
118121
// replace old IDs
119122
const idRegex = /[0-9a-f]{24}/g

0 commit comments

Comments
 (0)