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 resolversvalidations
andhooks
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)">"""</span></span>
<span><span style="color: var(--shiki-token-string-expression)"> {</span></span>
<span><span style="color: var(--shiki-token-string-expression)"> "</span><span style="color: var(--shiki-color-text)">objectIDs</span><span style="color: var(--shiki-token-string-expression)">": args.collection.filter(r, r.lang.contains("</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)">")).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)">""""</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)">// → { "a": 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 { "input": { "query": { "some": "thing" } } }</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 { "input": { "query": { "date": "2023-01-01" } } }</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)">// → "2023-01-01"</span></span>
<span></span>
<span><span style="color: var(--shiki-token-comment)">// args is { "input": { "query": { "some": "thing" } } }</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)">"1996-10-03"</span><span style="color: var(--shiki-color-text)">);</span></span>
<span><span style="color: var(--shiki-token-comment)">// → "1996-10-03"</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)">"a"</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)">"b"</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)">"c"</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)">"c"</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)">"z"</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)">"b"</span><span style="color: var(--shiki-color-text)">]);</span></span>
<span><span style="color: var(--shiki-token-comment)">// → ["b", "c"]</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)">"a"</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)">"b"</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)">// → { "a": 1, "b": 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 'x = 5, y = 7'</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)"><</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 'x = 5, y = 7'</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)">></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 'x = 5, y = 7'</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)">></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 { "input": { "query": { "some": "thing" } } }</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 { "input": { "query": { "some": 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>