Data validations

If Validate field is set in schema, validation rules will be applied to the field value. The data type of the field is an array of maps, with Expr and Action defined. The Action will be evaluated when the Expr returns true.

  • Expr: an expression in CEL Script or JavaScript.
  • Action: tailordb.#Permit.Allow or tailordb.#Permit.Deny.
  • ErrorMessage: a string of error message to be returned when the validation fails.
  • The validation rules are evaluated in descending order.
  • Note that Expr cannot access the fields provided through SourceId association.

Examples

<span><span style="color: var(--shiki-color-text)">reportNumber</span><span style="color: var(--shiki-token-punctuation)">:</span><span style="color: var(--shiki-color-text)"> {</span></span>
<span><span style="color: var(--shiki-color-text)">  Type</span><span style="color: var(--shiki-token-punctuation)">:</span><span style="color: var(--shiki-color-text)">        </span><span style="color: var(--shiki-token-constant)">tailordb</span><span style="color: var(--shiki-color-text)">.#TypeInt</span></span>
<span><span style="color: var(--shiki-color-text)">	Validate</span><span style="color: var(--shiki-token-punctuation)">:</span><span style="color: var(--shiki-color-text)"> [</span></span>
<span><span style="color: var(--shiki-color-text)">    </span><span style="color: var(--shiki-token-comment)">// reportNumber value must be less than 100 or over 103</span></span>
<span><span style="color: var(--shiki-color-text)">    {</span></span>
<span><span style="color: var(--shiki-color-text)">      Script</span><span style="color: var(--shiki-token-keyword)">:</span><span style="color: var(--shiki-color-text)"> </span><span style="color: var(--shiki-token-constant)">common</span><span style="color: var(--shiki-color-text)">.#Script </span><span style="color: var(--shiki-token-keyword)">&amp;</span><span style="color: var(--shiki-color-text)"> {</span></span>
<span><span style="color: var(--shiki-color-text)">        Expr</span><span style="color: var(--shiki-token-keyword)">:</span><span style="color: var(--shiki-color-text)"> </span><span style="color: var(--shiki-token-string-expression)">&quot;&quot;&quot;</span></span>
<span><span style="color: var(--shiki-token-string-expression)">          ((value, data) =&gt; {</span></span>
<span><span style="color: var(--shiki-color-text)">            return value </span><span style="color: var(--shiki-token-keyword)">&gt;=</span><span style="color: var(--shiki-token-constant)">100</span><span style="color: var(--shiki-color-text)"> </span><span style="color: var(--shiki-token-keyword)">&amp;&amp;</span><span style="color: var(--shiki-color-text)"> value </span><span style="color: var(--shiki-token-keyword)">&lt;=</span><span style="color: var(--shiki-token-constant)">103</span></span>
<span><span style="color: var(--shiki-color-text)">          })(_value</span><span style="color: var(--shiki-token-punctuation)">,</span><span style="color: var(--shiki-color-text)"> _data)</span></span>
<span><span style="color: var(--shiki-color-text)">        </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-color-text)">      Action: </span><span style="color: var(--shiki-token-constant)">tailordb</span><span style="color: var(--shiki-color-text)">.</span><span style="color: var(--shiki-token-constant)">#Permit</span><span style="color: var(--shiki-color-text)">.Deny</span></span>
<span><span style="color: var(--shiki-color-text)">      ErrorMessage: </span><span style="color: var(--shiki-token-string-expression)">&quot;reportNumber value must be less than 100 or over 103&quot;</span></span>
<span><span style="color: var(--shiki-color-text)">    }</span></span>
<span><span style="color: var(--shiki-color-text)">  ]</span></span>
<span><span style="color: var(--shiki-color-text)">}</span></span>
<span></span>
<span><span style="color: var(--shiki-color-text)">description</span><span style="color: var(--shiki-token-punctuation)">:</span><span style="color: var(--shiki-color-text)"> {</span></span>
<span><span style="color: var(--shiki-color-text)"> Type</span><span style="color: var(--shiki-token-punctuation)">:</span><span style="color: var(--shiki-color-text)">        </span><span style="color: var(--shiki-token-constant)">tailordb</span><span style="color: var(--shiki-color-text)">.#TypeString</span></span>
<span><span style="color: var(--shiki-color-text)"> Description</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;Description of the product.&quot;</span></span>
<span><span style="color: var(--shiki-color-text)"> Validate</span><span style="color: var(--shiki-token-punctuation)">:</span><span style="color: var(--shiki-color-text)"> [{</span></span>
<span><span style="color: var(--shiki-color-text)">   Script</span><span style="color: var(--shiki-token-keyword)">:</span><span style="color: var(--shiki-color-text)"> </span><span style="color: var(--shiki-token-constant)">common</span><span style="color: var(--shiki-color-text)">.#Script </span><span style="color: var(--shiki-token-keyword)">&amp;</span><span style="color: var(--shiki-color-text)"> {</span></span>
<span><span style="color: var(--shiki-color-text)">     Expr</span><span style="color: var(--shiki-token-keyword)">:</span><span style="color: var(--shiki-color-text)"> </span><span style="color: var(--shiki-token-string-expression)">&quot;&quot;&quot;</span></span>
<span><span style="color: var(--shiki-token-string-expression)">       ((value, data) =&gt; {</span></span>
<span><span style="color: var(--shiki-color-text)">         </span><span style="color: var(--shiki-token-function)">return</span><span style="color: var(--shiki-color-text)"> </span><span style="color: var(--shiki-token-keyword)">!</span><span style="color: var(--shiki-color-text)">(</span><span style="color: var(--shiki-token-constant)">value</span><span style="color: var(--shiki-color-text)">.</span><span style="color: var(--shiki-token-constant)">length</span><span style="color: var(--shiki-color-text)"> </span><span style="color: var(--shiki-token-keyword)">&lt;</span><span style="color: var(--shiki-color-text)"> </span><span style="color: var(--shiki-token-constant)">40</span><span style="color: var(--shiki-color-text)">)</span></span>
<span><span style="color: var(--shiki-color-text)">       })(_value</span><span style="color: var(--shiki-token-punctuation)">,</span><span style="color: var(--shiki-color-text)"> _data)</span></span>
<span><span style="color: var(--shiki-color-text)">     </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-color-text)">   Action: </span><span style="color: var(--shiki-token-constant)">tailordb</span><span style="color: var(--shiki-color-text)">.</span><span style="color: var(--shiki-token-constant)">#Permit</span><span style="color: var(--shiki-color-text)">.Deny</span></span>
<span><span style="color: var(--shiki-color-text)">   ErrorMessage: </span><span style="color: var(--shiki-token-string-expression)">&quot;Description length should be less than 40 characters.&quot;</span></span>
<span><span style="color: var(--shiki-color-text)"> }]</span></span>
<span><span style="color: var(--shiki-color-text)">}</span></span>
<span></span>
<span><span style="color: var(--shiki-color-text)">itemStatus</span><span style="color: var(--shiki-token-punctuation)">:</span><span style="color: var(--shiki-color-text)"> {</span></span>
<span><span style="color: var(--shiki-color-text)">  Type</span><span style="color: var(--shiki-token-punctuation)">:</span><span style="color: var(--shiki-color-text)"> </span><span style="color: var(--shiki-token-constant)">tailordb</span><span style="color: var(--shiki-color-text)">.#TypeString</span></span>
<span><span style="color: var(--shiki-color-text)">  Validate</span><span style="color: var(--shiki-token-punctuation)">:</span><span style="color: var(--shiki-color-text)"> [{</span></span>
<span><span style="color: var(--shiki-color-text)">    Script</span><span style="color: var(--shiki-token-keyword)">:</span><span style="color: var(--shiki-color-text)"> </span><span style="color: var(--shiki-token-constant)">common</span><span style="color: var(--shiki-color-text)">.#Script </span><span style="color: var(--shiki-token-keyword)">&amp;</span><span style="color: var(--shiki-color-text)"> {</span></span>
<span><span style="color: var(--shiki-color-text)">      Expr</span><span style="color: var(--shiki-token-keyword)">:</span><span style="color: var(--shiki-color-text)"> </span><span style="color: var(--shiki-token-string-expression)">&quot;&quot;&quot;</span></span>
<span><span style="color: var(--shiki-token-string-expression)">        ((value, data) =&gt; {</span></span>
<span><span style="color: var(--shiki-color-text)">          return </span><span style="color: var(--shiki-token-constant)">Object</span><span style="color: var(--shiki-token-function)">.keys</span><span style="color: var(--shiki-color-text)">(user).</span><span style="color: var(--shiki-token-constant)">length</span><span style="color: var(--shiki-color-text)"> </span><span style="color: var(--shiki-token-keyword)">===</span><span style="color: var(--shiki-color-text)"> </span><span style="color: var(--shiki-token-constant)">0</span></span>
<span><span style="color: var(--shiki-color-text)">         })(_value</span><span style="color: var(--shiki-token-punctuation)">,</span><span style="color: var(--shiki-color-text)"> _data)</span></span>
<span><span style="color: var(--shiki-color-text)">        </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-color-text)">    Action:       </span><span style="color: var(--shiki-token-constant)">tailordb</span><span style="color: var(--shiki-color-text)">.</span><span style="color: var(--shiki-token-constant)">#Permit</span><span style="color: var(--shiki-color-text)">.Deny</span></span>
<span><span style="color: var(--shiki-color-text)">    ErrorMessage: </span><span style="color: var(--shiki-token-string-expression)">&quot;You must be logged in to create an item&quot;</span></span>
<span><span style="color: var(--shiki-color-text)">  }]</span></span>
<span><span style="color: var(--shiki-color-text)">}</span></span>
<span></span>

Example demonstrating how to use user attributes

<span><span style="color: var(--shiki-color-text)">itemCode</span><span style="color: var(--shiki-token-punctuation)">:</span><span style="color: var(--shiki-color-text)"> {</span></span>
<span><span style="color: var(--shiki-color-text)">  Type</span><span style="color: var(--shiki-token-punctuation)">:</span><span style="color: var(--shiki-color-text)"> </span><span style="color: var(--shiki-token-constant)">tailordb</span><span style="color: var(--shiki-color-text)">.#TypeString</span></span>
<span><span style="color: var(--shiki-color-text)"> 	Validate</span><span style="color: var(--shiki-token-punctuation)">:</span><span style="color: var(--shiki-color-text)"> [</span></span>
<span><span style="color: var(--shiki-color-text)">    {</span></span>
<span><span style="color: var(--shiki-color-text)">      Script</span><span style="color: var(--shiki-token-keyword)">:</span><span style="color: var(--shiki-color-text)"> </span><span style="color: var(--shiki-token-constant)">common</span><span style="color: var(--shiki-color-text)">.#Script </span><span style="color: var(--shiki-token-keyword)">&amp;</span><span style="color: var(--shiki-color-text)"> {</span></span>
<span><span style="color: var(--shiki-color-text)">        Expr</span><span style="color: var(--shiki-token-keyword)">:</span><span style="color: var(--shiki-color-text)"> </span><span style="color: var(--shiki-token-string-expression)">&quot;&quot;&quot;</span></span>
<span><span style="color: var(--shiki-token-string-expression)">          ((value, data) =&gt; {</span></span>
<span><span style="color: var(--shiki-color-text)">            return </span><span style="color: var(--shiki-token-keyword)">!</span><span style="color: var(--shiki-token-constant)">user</span><span style="color: var(--shiki-token-function)">.</span><span style="color: var(--shiki-token-constant)">attributes</span><span style="color: var(--shiki-token-function)">.includes</span><span style="color: var(--shiki-color-text)">(</span><span style="color: var(--shiki-token-string-expression)">&quot;&lt;ADMIN-ID&gt;&quot;</span><span style="color: var(--shiki-color-text)">)</span></span>
<span><span style="color: var(--shiki-color-text)">          })(_value</span><span style="color: var(--shiki-token-punctuation)">,</span><span style="color: var(--shiki-color-text)"> _data)</span></span>
<span><span style="color: var(--shiki-color-text)">        </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-color-text)">      Action:       </span><span style="color: var(--shiki-token-constant)">tailordb</span><span style="color: var(--shiki-color-text)">.</span><span style="color: var(--shiki-token-constant)">#Permit</span><span style="color: var(--shiki-color-text)">.Deny</span></span>
<span><span style="color: var(--shiki-color-text)">      ErrorMessage: </span><span style="color: var(--shiki-token-string-expression)">&quot;To create an item, you must be the Admin&quot;</span></span>
<span><span style="color: var(--shiki-color-text)">    }</span></span>
<span><span style="color: var(--shiki-color-text)">  ]</span></span>
<span><span style="color: var(--shiki-color-text)">}</span></span>
<span></span>

Accessible variables in expressions

VariableDescription
_valueThis accesses its own field value.
_value.{other_field_name}Other fields can be accessed using the field name. ex. foo == 2 accesses foo field
_value.{array_name}[{index}]This accesses the array value. ex. arr[0] accesses the first value of arr
_value.{associative_array_name}.{key_name}This accesses associative array value. ex. dict.nestedValue accesses the dict associative array's nestedValue key value
userThis accesses the user context. ex. user.id accesses the logged in user id and user.attributes acessess user's attributes which is an array of UUIDs that are configured in the AttributesFields in the Auth service

Embedded functions in expressions

FunctionDescription
today()returns today's date in String, with format YYYY-MM-DD. ex. today() => "2024-07-02"
currentHHMM()returns the current time in String, with format HH:MM. ex. "21:37"
timeHH(int)returns the hours in int from the provided time. ex. timeHH("1213") => 12
timeMM(int)returns the minutes in int from the provided time. ex. timeMM("1213") => 13
startWith(string, string)returns the Boolean value which is the result of string prefix match. ex. startWith("hello world", "hello") => true
endWith(string, string)returns the Boolean value which is the result of string suffix match. ex. endWith("hello world", "world") => true
length(string)returns the int which is the length of the string. ex. length("12345") => 5