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: one of the following: tailordb.#Permit.Allow or tailordb.#Permit.Deny or tailordb.#Permit.Skip.
  • 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

reportNumber: {
  Type:        tailordb.#TypeInt
	Validate: [
    // reportNumber value must be less than 100 or over 103
    {
      Script: common.#Script & {
        Expr: """
          ((value, data) => {
            return value >=100 && value <=103
          })(_value, _data)
        """
      }
      Action: tailordb.#Permit.Deny
      ErrorMessage: "reportNumber value must be less than 100 or over 103"
    }
  ]
}
description: {
  Type:        tailordb.#TypeString
  Description: "Description of the product."
  Validate: [{
    Script: common.#Script & {
      Expr: """
        ((value, data) => {
          return !(value.length < 40)
        })(_value, _data)
      """
    }
    Action: tailordb.#Permit.Deny
    ErrorMessage: "Description length should be less than 40 characters."
  }]
}
itemStatus: {
  Type: tailordb.#TypeString
  Validate: [{
    Script: common.#Script & {
      Expr: """
        ((value, data) => {
          return Object.keys(user).length === 0
         })(_value, _data)
        """
      }
    Action:       tailordb.#Permit.Deny
    ErrorMessage: "You must be logged in to create an item"
  }]
}

Example demonstrating how to use user attributes


itemCode: {
  Type: tailordb.#TypeString
 	Validate: [
    {
      Script: common.#Script & {
        Expr: """
          ((value, data) => {
            return !user.attributes.includes("<ADMIN-ID>")
          })(_value, _data)
        """
      }
      Action:       tailordb.#Permit.Deny
      ErrorMessage: "To create an item, you must be the Admin"
    }
  ]
}

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 date in int64, with format YYYYMMDD. ex) today() => 20230102
addDate(int64, int64)returns the date in int64, adding the second argument days to the first argument date. ex) addDate(today(), 1) => 20230103
currentHHMM()returns the current time in int64, with format HHMM.
timeHH(int64)returns the current time in int64, with format HHMM.
timeMM(int64)returns the minutes in int64 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 int64 which is the length of the string. ex.) length("12345") => 5