Tagscript

SafeObjectTransformer

Expose a plain object by dotted path, with private keys refused.

SafeObjectTransformer exposes an object's fields to a template. Nested fields come back through a dotted path, and any key starting with an underscore is refused.

For template authors

Syntax

{name(key)}
{name(nested.key)}
{name.key}

Examples

Given u holds { "name": "Parbez", "address": { "city": "Silchar" } }:

{u(name)}
# Parbez

{u(address.city)}
# Silchar

A key that does not exist, or one starting with an underscore, leaves the tag as written:

{u(_secret)}
# {u(_secret)}

The json tag creates one of these from inside a template.

For developers

Seeding one

import { Interpreter, SafeObjectTransformer, StrictVarsParser } from 'tagscript';

const ts = new Interpreter(new StrictVarsParser());

const response = await ts.run('Hi {user(name)}', {
	user: new SafeObjectTransformer({ name: 'Parbez', address: { city: 'Silchar' } }),
});

The constructor takes an object or a JSON string.

What makes it safe

The constructor runs the input through JSON.parse(JSON.stringify(value)). Methods, getters, prototype links and anything else JSON.stringify drops do not survive, so the template gets a data-only copy. transform then refuses any parameter starting with _.

The copy is made once, in the constructor. Changing the original object after that has no effect on what the template renders. The flip side is that a large object is fully serialised up front, so keep what you seed small.

Rendering the bare {name} with no parameter calls toString on the original value, which for a plain object gives [object Object]. Point templates at a specific key.

API reference

SafeObjectTransformer

Last updated on

On this page

Edit on Github