Skip to content
Docs

How-to: Conditional logic

Publicus has two conditional primitives:

  • Switch routes execution down one of several branches. Cases are evaluated in order and the first matching case wins, like an if/else-if/else chain.
  • ConditionalExit is a guard: if its condition is met the flow stops at that point; otherwise the flow continues.

Use Switch when different data should take different paths. Use ConditionalExit when the only alternative to continuing is stopping.

This guide covers the main patterns.

Add a Switch node where the flow should split, then draw an edge from the Switch’s output to the first node of each branch.

Configure the Switch with an ordered list of cases. Each case has:

  • a label that names the branch (shown in the run output, so make it descriptive),
  • a condition expression that resolves truthy or falsy, using the same syntax as a ConditionalExit condition,
  • a target node, the first node of the branch to take when this case is the first to match.

Cases are evaluated top to bottom. The first truthy condition wins and execution continues down that case’s branch only.

┌→ Mapper (purchase) → ...
Webhook → Switch ───┼→ Mapper (refund) → ...
└→ Mapper (other) → ... (default)
case 1: label: purchase condition: $start->payload->type == "purchase"
case 2: label: refund condition: $start->payload->type == "refund"
default: the "other" branch

Conditions are bare boolean expressions, without {{ }} braces. In a webhook flow the request body arrives under $start->payload, so the event type is $start->payload->type.

The default setting names the branch to take when no case matches, acting as the else. Leave it empty and the run ends at the Switch instead, which turns the Switch into a multi-way guard.

Every branch that is not chosen is skipped: its nodes never run and produce no entries in the run report. If branches merge back into a shared node downstream, that shared node still runs.

The Switch itself outputs which branch it picked, including the matched case’s label, so the run report shows why the run went the way it did.

A run report where the By weight Switch outputs the chosen branch with matchedLabel Over a tonne, and only Book a truck runs after it
A run report where the By weight Switch outputs the chosen branch with matchedLabel Over a tonne, and only Book a truck runs after it

HTTP nodes (REST and OpenAPI operations) can be configured to continue on upstream errors instead of failing the run. In that mode an error produces a structured output with the error message and upstream status code, which a downstream Switch can branch on:

case 1: label: upstream-error condition: $prev->error ?? false
default: the happy-path branch

A successful response has no error field, so null-coalesce it to false for the happy path.

A run report where a Rest Operation that got a 404 completes with an error envelope: error true, upstreamStatus 404, circuitOpen false and the message
A run report where a Rest Operation that got a 404 completes with an error envelope: error true, upstreamStatus 404, circuitOpen false and the message

This gives you a dedicated error-handling branch (alert, retry later, write to a store) without failing the whole flow.

Place a ConditionalExit after any node to stop the flow when the upstream output does not meet your criteria. The condition is the exit predicate: write it as the reason to stop.

Webhook → ConditionalExit → ... rest of flow ...

Configure the condition:

condition: $start->payload->type != "purchase"
message: Not a purchase event — skipping.

The flow stops for every event that is not a purchase; purchase events fall through and continue.

Each condition below is truthy exactly when the flow should stop.

condition: count($prev) == 0
message: No matching records found.

$prev is the query’s result array. When no rows match, the array is empty, so count($prev) == 0 is truthy and the flow stops.

condition: ($prev->email ?? '') == ''
message: Record has no email — skipping.

Stop when an API response indicates no more data

Section titled “Stop when an API response indicates no more data”
condition: !($prev->hasMore ?? false)
message: All pages fetched — done.

Deduplicate: stop if the record already exists

Section titled “Deduplicate: stop if the record already exists”
  1. Add a TableStore / Query to check for the record.

  2. Wire its output into a ConditionalExit:

    condition: count($prev) > 0
    message: Record already exists — skipping insert.
    • The query found the record → the condition is truthy → the flow stops.
    • The result array is empty → the condition is falsy → the flow continues to the insert.

    That is the right logic for a deduplication guard.

Choosing between Switch and ConditionalExit

Section titled “Choosing between Switch and ConditionalExit”

Reach for a Switch when there is more than one way forward. Reach for a ConditionalExit when the flow should either continue or stop, for example a dedupe check or a feature-flag gate. A ConditionalExit keeps the graph linear and records a message explaining why the run stopped; a Switch with an empty default can end a run too, but a guard says so more clearly.

The two combine well: guard early with ConditionalExit to filter out runs that should not happen at all, then Switch later to route the runs that remain.

Use KeyValueStore / Get → ConditionalExit to gate a flow on a runtime flag:

condition: $prev != "enabled"
message: Feature flag disabled — skipping.

KeyValueStore / Get outputs the stored value itself, so $prev compares against it directly. You can also skip the Get node and read the flag straight from the $kv root in the condition: $kv->featureFlag != "enabled".

Change the KV entry value to "enabled" or "disabled" from the Storage UI without touching the flow.

The message is written to the run’s record when the condition is truthy — that is, when the flow exits at the guard. Use it to explain why the flow stopped so you can understand the run report without re-reading the configuration:

message: Account {{ $start->accountId }} is not on the paid plan, skipping export.

Expressions are resolved in the message field too.

A run report where the Gate via KV ConditionalExit stopped the run, its output recording condition true and its message that dispatch is paused because the gate key is off
A run report where the Gate via KV ConditionalExit stopped the run, its output recording condition true and its message that dispatch is paused because the gate key is off