Skip to content

Commit 81348f4

Browse files
authored
Merge pull request #2986 from hapijs/fix/missing-template-reference
fix: missing template reference
2 parents 89d7d51 + df7f8d2 commit 81348f4

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

Diff for: lib/template.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,11 @@ module.exports = exports = internals.Template = class {
207207

208208
const ref = Ref.create(variable, this._settings);
209209
refs.push(ref);
210-
return (context) => ref.resolve(...context);
210+
return (context) => {
211+
212+
const resolved = ref.resolve(...context);
213+
return resolved !== undefined ? resolved : null;
214+
};
211215
};
212216

213217
try {

Diff for: test/template.js

+11
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,17 @@ describe('Template', () => {
8585
expect(template.render({}, {}, { context: { x: 'hello', y: '!' } }, {}, { errors: { escapeHtml: false } })).to.equal('text hello! {{escaped}} xxx abc {{{ignore}} 123 {{x');
8686
});
8787

88+
it('parses template with missing elements in binary operation', () => {
89+
90+
const source = 'text {$x || $y}';
91+
const template = Joi.x(source);
92+
93+
expect(template.source).to.equal(source);
94+
expect(template.render({}, {}, { context: { x: 'hello' } })).to.equal('text hello');
95+
expect(template.render({}, {}, { context: { y: 'hello' } })).to.equal('text hello');
96+
expect(template.render({}, {}, { context: {} })).to.equal('text null');
97+
});
98+
8899
it('parses template with single variable', () => {
89100

90101
const source = '{$x}';

0 commit comments

Comments
 (0)