Skip to content

Commit

Permalink
Merge pull request #2986 from hapijs/fix/missing-template-reference
Browse files Browse the repository at this point in the history
fix: missing template reference
  • Loading branch information
Marsup committed Sep 17, 2023
2 parents 89d7d51 + df7f8d2 commit 81348f4
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/template.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,11 @@ module.exports = exports = internals.Template = class {

const ref = Ref.create(variable, this._settings);
refs.push(ref);
return (context) => ref.resolve(...context);
return (context) => {

const resolved = ref.resolve(...context);
return resolved !== undefined ? resolved : null;
};
};

try {
Expand Down
11 changes: 11 additions & 0 deletions test/template.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,17 @@ describe('Template', () => {
expect(template.render({}, {}, { context: { x: 'hello', y: '!' } }, {}, { errors: { escapeHtml: false } })).to.equal('text hello! {{escaped}} xxx abc {{{ignore}} 123 {{x');
});

it('parses template with missing elements in binary operation', () => {

const source = 'text {$x || $y}';
const template = Joi.x(source);

expect(template.source).to.equal(source);
expect(template.render({}, {}, { context: { x: 'hello' } })).to.equal('text hello');
expect(template.render({}, {}, { context: { y: 'hello' } })).to.equal('text hello');
expect(template.render({}, {}, { context: {} })).to.equal('text null');
});

it('parses template with single variable', () => {

const source = '{$x}';
Expand Down

0 comments on commit 81348f4

Please sign in to comment.