CEL scripting

CEL Script is used to perform data transformation and run validations in the Tailor Platform.

Here are some places you can utilize CEL:

  • authorization, preScript, postScript, preValidation, postValidation blocks on Pipeline resolvers
  • validations and hooks blocks on TailorDB.

Standard operators and functions as well as String extensions can be used.

Example:

<span><span style="color: var(--shiki-token-comment)">// filter and extract the IDs of a collection</span></span>
<span><span style="color: var(--shiki-color-text)">preScript: </span><span style="color: var(--shiki-token-string-expression)">&quot;&quot;&quot;</span></span>
<span><span style="color: var(--shiki-token-string-expression)">  {</span></span>
<span><span style="color: var(--shiki-token-string-expression)">    &quot;</span><span style="color: var(--shiki-color-text)">objectIDs</span><span style="color: var(--shiki-token-string-expression)">&quot;: args.collection.filter(r, r.lang.contains(&quot;</span><span style="color: var(--shiki-color-text)">en</span><span style="color: var(--shiki-token-keyword)">-</span><span style="color: var(--shiki-color-text)">US</span><span style="color: var(--shiki-token-string-expression)">&quot;)).map(r, r.id)</span></span>
<span><span style="color: var(--shiki-token-string-expression)">  }</span></span>
<span><span style="color: var(--shiki-token-string-expression)">&quot;&quot;&quot;&quot;</span></span>
<span></span>

Tailor Platform extensions

In addition, the Tailor platform provides useful extensions.

compact

Accepts a map and returns a new one that excludes the entries that have null values.

<span><span style="color: var(--shiki-token-function)">compact</span><span style="color: var(--shiki-color-text)">({ a</span><span style="color: var(--shiki-token-keyword)">:</span><span style="color: var(--shiki-color-text)"> </span><span style="color: var(--shiki-token-constant)">1</span><span style="color: var(--shiki-token-punctuation)">,</span><span style="color: var(--shiki-color-text)"> b</span><span style="color: var(--shiki-token-keyword)">:</span><span style="color: var(--shiki-color-text)"> </span><span style="color: var(--shiki-token-constant)">null</span><span style="color: var(--shiki-color-text)"> });</span></span>
<span><span style="color: var(--shiki-token-comment)">// → { &quot;a&quot;: 1 }</span></span>
<span></span>

get

Provides safe navigation. Accepts an object, a path, and fallback value.

<span><span style="color: var(--shiki-token-comment)">// args is { &quot;input&quot;: { &quot;query&quot;: { &quot;some&quot;: &quot;thing&quot; } } }</span></span>
<span><span style="color: var(--shiki-token-function)">get</span><span style="color: var(--shiki-color-text)">(</span><span style="color: var(--shiki-token-constant)">args</span><span style="color: var(--shiki-color-text)">.</span><span style="color: var(--shiki-token-constant)">input</span><span style="color: var(--shiki-color-text)">.</span><span style="color: var(--shiki-token-constant)">query</span><span style="color: var(--shiki-color-text)">.date);</span></span>
<span><span style="color: var(--shiki-token-comment)">// → null</span></span>
<span></span>
<span><span style="color: var(--shiki-token-comment)">// args is { &quot;input&quot;: { &quot;query&quot;: { &quot;date&quot;: &quot;2023-01-01&quot; } } }</span></span>
<span><span style="color: var(--shiki-token-function)">get</span><span style="color: var(--shiki-color-text)">(</span><span style="color: var(--shiki-token-constant)">args</span><span style="color: var(--shiki-color-text)">.</span><span style="color: var(--shiki-token-constant)">input</span><span style="color: var(--shiki-color-text)">.</span><span style="color: var(--shiki-token-constant)">query</span><span style="color: var(--shiki-color-text)">.date);</span></span>
<span><span style="color: var(--shiki-token-comment)">// → &quot;2023-01-01&quot;</span></span>
<span></span>
<span><span style="color: var(--shiki-token-comment)">// args is { &quot;input&quot;: { &quot;query&quot;: { &quot;some&quot;: &quot;thing&quot; } } }</span></span>
<span><span style="color: var(--shiki-token-function)">get</span><span style="color: var(--shiki-color-text)">(</span><span style="color: var(--shiki-token-constant)">args</span><span style="color: var(--shiki-color-text)">.</span><span style="color: var(--shiki-token-constant)">input</span><span style="color: var(--shiki-color-text)">.</span><span style="color: var(--shiki-token-constant)">query</span><span style="color: var(--shiki-color-text)">.date</span><span style="color: var(--shiki-token-punctuation)">,</span><span style="color: var(--shiki-color-text)"> </span><span style="color: var(--shiki-token-string-expression)">&quot;1996-10-03&quot;</span><span style="color: var(--shiki-color-text)">);</span></span>
<span><span style="color: var(--shiki-token-comment)">// → &quot;1996-10-03&quot;</span></span>
<span></span>

intersect

Accepts two arrays and returns an array which contains the elements found in both. Drops null values.

<span><span style="color: var(--shiki-token-function)">intersect</span><span style="color: var(--shiki-color-text)">([</span><span style="color: var(--shiki-token-constant)">1</span><span style="color: var(--shiki-token-punctuation)">,</span><span style="color: var(--shiki-color-text)"> </span><span style="color: var(--shiki-token-constant)">2</span><span style="color: var(--shiki-token-punctuation)">,</span><span style="color: var(--shiki-color-text)"> </span><span style="color: var(--shiki-token-constant)">3</span><span style="color: var(--shiki-token-punctuation)">,</span><span style="color: var(--shiki-color-text)"> </span><span style="color: var(--shiki-token-constant)">null</span><span style="color: var(--shiki-color-text)">]</span><span style="color: var(--shiki-token-punctuation)">,</span><span style="color: var(--shiki-color-text)"> [</span><span style="color: var(--shiki-token-constant)">null</span><span style="color: var(--shiki-token-punctuation)">,</span><span style="color: var(--shiki-color-text)"> </span><span style="color: var(--shiki-token-constant)">3</span><span style="color: var(--shiki-token-punctuation)">,</span><span style="color: var(--shiki-color-text)"> </span><span style="color: var(--shiki-token-constant)">4</span><span style="color: var(--shiki-token-punctuation)">,</span><span style="color: var(--shiki-color-text)"> </span><span style="color: var(--shiki-token-constant)">5</span><span style="color: var(--shiki-color-text)">]);</span></span>
<span><span style="color: var(--shiki-token-comment)">// → [3]</span></span>
<span></span>
<span><span style="color: var(--shiki-token-function)">intersect</span><span style="color: var(--shiki-color-text)">([</span><span style="color: var(--shiki-token-string-expression)">&quot;a&quot;</span><span style="color: var(--shiki-token-punctuation)">,</span><span style="color: var(--shiki-color-text)"> </span><span style="color: var(--shiki-token-string-expression)">&quot;b&quot;</span><span style="color: var(--shiki-token-punctuation)">,</span><span style="color: var(--shiki-color-text)"> </span><span style="color: var(--shiki-token-string-expression)">&quot;c&quot;</span><span style="color: var(--shiki-color-text)">]</span><span style="color: var(--shiki-token-punctuation)">,</span><span style="color: var(--shiki-color-text)"> [</span><span style="color: var(--shiki-token-string-expression)">&quot;c&quot;</span><span style="color: var(--shiki-token-punctuation)">,</span><span style="color: var(--shiki-color-text)"> </span><span style="color: var(--shiki-token-string-expression)">&quot;z&quot;</span><span style="color: var(--shiki-token-punctuation)">,</span><span style="color: var(--shiki-color-text)"> </span><span style="color: var(--shiki-token-string-expression)">&quot;b&quot;</span><span style="color: var(--shiki-color-text)">]);</span></span>
<span><span style="color: var(--shiki-token-comment)">// → [&quot;b&quot;, &quot;c&quot;]</span></span>
<span></span>

makeMap

Accepts an array and generates a map from it.

<span><span style="color: var(--shiki-token-function)">makeMap</span><span style="color: var(--shiki-color-text)">([</span><span style="color: var(--shiki-token-string-expression)">&quot;a&quot;</span><span style="color: var(--shiki-token-punctuation)">,</span><span style="color: var(--shiki-color-text)"> </span><span style="color: var(--shiki-token-constant)">1</span><span style="color: var(--shiki-token-punctuation)">,</span><span style="color: var(--shiki-color-text)"> </span><span style="color: var(--shiki-token-string-expression)">&quot;b&quot;</span><span style="color: var(--shiki-token-punctuation)">,</span><span style="color: var(--shiki-color-text)"> </span><span style="color: var(--shiki-token-constant)">2</span><span style="color: var(--shiki-color-text)">]);</span></span>
<span><span style="color: var(--shiki-token-comment)">// → { &quot;a&quot;: 1, &quot;b&quot;: 2 }</span></span>
<span></span>

uuidgen

Generates a UUID v4 string.

<span><span style="color: var(--shiki-token-function)">uuidgen</span><span style="color: var(--shiki-color-text)">();</span></span>
<span><span style="color: var(--shiki-token-comment)">// → 4f88b002-859f-4f59-b6fd-521c75041b13</span></span>
<span></span>

ifThen

Returns a specific value based on evaluation.

Accepts a condition, a true value and false value. If the condition evaluates to true, the true value is returned. Otherwise, the false value is returned.

<span><span style="color: var(--shiki-token-comment)">// args is &#39;x = 5, y = 7&#39;</span></span>
<span><span style="color: var(--shiki-token-function)">ifThen</span><span style="color: var(--shiki-color-text)">(x </span><span style="color: var(--shiki-token-keyword)">&lt;</span><span style="color: var(--shiki-color-text)"> y</span><span style="color: var(--shiki-token-punctuation)">,</span><span style="color: var(--shiki-color-text)"> x</span><span style="color: var(--shiki-token-punctuation)">,</span><span style="color: var(--shiki-color-text)"> y);</span></span>
<span><span style="color: var(--shiki-token-comment)">// → 5</span></span>
<span></span>
<span><span style="color: var(--shiki-token-comment)">// args is &#39;x = 5, y = 7&#39;</span></span>
<span><span style="color: var(--shiki-token-function)">ifThen</span><span style="color: var(--shiki-color-text)">(x </span><span style="color: var(--shiki-token-keyword)">&gt;</span><span style="color: var(--shiki-color-text)"> y</span><span style="color: var(--shiki-token-punctuation)">,</span><span style="color: var(--shiki-color-text)"> x</span><span style="color: var(--shiki-token-punctuation)">,</span><span style="color: var(--shiki-color-text)"> y);</span></span>
<span><span style="color: var(--shiki-token-comment)">// → 7</span></span>
<span></span>
<span><span style="color: var(--shiki-token-comment)">// args is &#39;x = 5, y = 7&#39;</span></span>
<span><span style="color: var(--shiki-token-function)">ifThen</span><span style="color: var(--shiki-color-text)">(x </span><span style="color: var(--shiki-token-keyword)">&gt;</span><span style="color: var(--shiki-color-text)"> y</span><span style="color: var(--shiki-token-punctuation)">,</span><span style="color: var(--shiki-color-text)"> x);</span></span>
<span><span style="color: var(--shiki-token-comment)">// → null</span></span>
<span></span>

isNull

Evaluates whether input is null and returns true or false.

<span><span style="color: var(--shiki-token-comment)">// args is { &quot;input&quot;: { &quot;query&quot;: { &quot;some&quot;: &quot;thing&quot; } } }</span></span>
<span><span style="color: var(--shiki-token-function)">isNull</span><span style="color: var(--shiki-color-text)">(</span><span style="color: var(--shiki-token-constant)">args</span><span style="color: var(--shiki-color-text)">.</span><span style="color: var(--shiki-token-constant)">input</span><span style="color: var(--shiki-color-text)">.</span><span style="color: var(--shiki-token-constant)">query</span><span style="color: var(--shiki-color-text)">.some);</span></span>
<span><span style="color: var(--shiki-token-comment)">// → false</span></span>
<span></span>
<span><span style="color: var(--shiki-token-comment)">// args is { &quot;input&quot;: { &quot;query&quot;: { &quot;some&quot;: null } } }</span></span>
<span><span style="color: var(--shiki-token-function)">isNull</span><span style="color: var(--shiki-color-text)">(</span><span style="color: var(--shiki-token-constant)">args</span><span style="color: var(--shiki-color-text)">.</span><span style="color: var(--shiki-token-constant)">input</span><span style="color: var(--shiki-color-text)">.</span><span style="color: var(--shiki-token-constant)">query</span><span style="color: var(--shiki-color-text)">.some);</span></span>
<span><span style="color: var(--shiki-token-comment)">// → true</span></span>
<span></span>