Guard

Guard functions control whether the main function should execute, based on runtime conditions such as query results, server responses, or business logic rules.

A guard function runs after any defined queries and before the main REST call is executed. It can call another function to validate the current app state, perform a check against local or remote data, and return a result that determines whether to proceed with the main function or not.

The outcome of the guard function, whether it succeeds or fails, determines the next steps in your app's flow.

What guard functions do

  • Act as a pre-check before executing the main function.

  • Help prevent actions under incorrect conditions (e.g., stale data, duplicate submissions).

  • Evaluate dynamic conditions based on local queries, app parameters, or API responses.

  • Optionally trigger fallback operations if the check fails.

When to use guard functions

Technical issues - Prevent REST calls when a condition already exists—for example, checking if a record already exists on the server to avoid duplicate entries.

Business Logic - Verify preconditions, like checking if an appointment is still open before allowing status updates, especially useful in offline-first or multi-device environments.

Example: When completing an appointment, a guard function can check if the appointment’s status is still open. If the status has changed to closed on the server, the guard function returns false and shows an error message, preventing invalid updates.

Execution flow

When used in a function:

  1. The guard function is executed first (if enabled for initial/continuation/retry).

  2. If the guard succeeds, the main function proceeds as normal.

  3. If the guard fails, the main function is skipped, and any defined operations in the guard are executed instead.

  4. The guard’s output is available using [email protected].

Execution timing options

Property
Purpose

onInitial

Runs guard before the initial REST call.

onContinuation

Re-evaluates guard on function continuation.

onRetry

Runs guard before retrying a failed function.

Guard Output

  • Use the [email protected] expression to get the output from the guard function.

  • The output is never saved to a table.

  • The output can be used when chaining function calls.

  • If the output is not explicitly used, the default provider's output is used, and in the case of the REST provider, that would be the body.

Configuration options

Properties
Description

function

The name or functionId of the function to execute as the guard, before the main function, to determine whether the main function should be executed.

Parameters

parameters passed to the guard function from the specified function.

onContinuation

Determine if the guard function should be executed on a continuation call. Defaults to true.

onInitial

Determines whether the guard function executes before the first call to the REST function. Default is true. If set to false, the guard function will execute in the defined execution order.

onRetry

Determines whether the function should execute a retry call. Defaults to true.

operations

Operations to run if the guard function fails. Used to update local data, show messages, or log state.

result

The result of the guard function is evaluated to determine whether the main function should be executed. If the guard function fails or the result of the expression is falsy, the main function is not executed. The output of the guard function is available to expressions and scripts using [email protected]

when

Condition to determine if the guard function itself should execute.

Expressions

Expression
Description

output

Output of the guard function is returned to the function as actions. If specified, the output of the function is the result of the expression; otherwise, the output is defined as the default output of the REST provider.

Example code

provider: DATA_PROVIDER_REST
method: GET
url: url
useLocalCall: true

guard:
  # NOTE: the main function when is checked before the guard function is evaluated,
  # the guard when checks whether to call the guard function.
  # the guard result expression determines if the main function should be executed or not.
  # the guard operations are executed if the guard function fails or returns a falsy value.
  function: check-book-title-local-rest
  parameters:
    title: [email protected]
  # Should the guard function be executed at all (default: true)
  when: true
  # Should the guard function be executer before the first (initial) call of the main function (default: true)
  onInitial: true
  # Should the guard function be executed before each continuation of the main function (default: true)
  onContinuation: true
  # Should the guard function be executed before each retry of the main function (default: true)
  onRetry: true
  # The result expression is evaluated in the context of the guard function to determine if the main function should be executed (true) or not (false).
  result: "[email protected] ? true : false"
  operations:
    - type: operation.execute-sql
      statements:
        - statement: |
            UPDATE _commandQueue
            SET payload = REPLACE(payload, @tempId, @id)
            WHERE payload LIKE '%' || @tempId || '%'
          parameters:
            tempId: ='_tmp_' & $string(@.commandId) & '_'
            id: [email protected]
        - statement: |
            ="UPDATE [" & @.entity & "]\nSET [id] = REPLACE([id], @tempId, @id),\n[data] = REPLACE([data], @tempID, @id)\nWHERE ([data] LIKE '%' || @tempId || '%') OR ([id] = @tempId)"
          parameters:
            tempId: ='_tmp_' & $string(@.commandId) & '_'
            id: [email protected]
        - statement: |
            DELETE FROM _commandQueue
            WHERE [id] = @commandId
          parameters:
            commandId: [email protected]
      tables:
        - _commandQueue
        - [email protected]
 

Last updated

Was this helpful?