# If Condition

> Evaluates a boolean condition against the input payload to bifurcate the execution flow.

Source: https://docs.sommatic.ai/en/nodes/flow.if/

- Slug: `flow.if`
- Family: Flow control
- Version: 1.0.0
- Status: Available
- Execution: sync

Evaluates a boolean condition against the input payload to bifurcate the execution flow.

This operator functions as a gateway that routes execution to either the 'true' or 'false' port based on the result of the configured expression. It supports two evaluation modes: the Sommatic AI Expression Engine ({{ }} syntax, full JavaScript via vm.Script) as the primary engine, and raw JSONata as a simpler fallback for expressions entered without {{ }}.

**Behavior:**
- If the expression evaluates to a truthy value, execution proceeds through the 'true' port.
- If the expression evaluates to a falsy value, execution proceeds through the 'false' port.
- If 'treat_missing_as' is set to true and the expression yields undefined/null, it is treated as false.

## Configuration

- `expr` (string, required, accepts expressions): An expression that must evaluate to a boolean. Wrap it in {{ }} to use the Sommatic AI Expression Engine (JavaScript) with variables like $json, $input or $("Node Name"), e.g. {{ $json.age >= 18 }} or {{ $json.status === "active" }}. Without {{ }} the text is evaluated as JSONata against the payload, where fields are referenced directly: age >= 18 or status = "active". $json is not bound in JSONata, so $json.age >= 18 without {{ }} always evaluates to false.
- `treat_missing_as` (boolean, optional): If enabled, undefined or null results from the expression will automatically route to the False port instead of throwing an error. Default: `false`.

Its configuration shows the shared execution settings, but this node does not apply retries (`retryOnFail`, `maxTries`) yet. `continueOnFail` is not applied by any node.

## Output ports

- `true` (default): Active when expression is true.
- `false`: Active when expression is false.
- `error`: Active when expression evaluation fails.

## Input contract

- `payload` (object, optional)

## Output contract

- `payload` (object, optional)

## In expressions

What a later node can read from this node's output with `$("If Condition").item.json.<path>` (write your node's label instead of `If Condition`). **Editor** marks the paths the workflow editor suggests under Previous Nodes (the output contract); **Worker** marks the paths the worker really returns through `true`, from the output shown in Ports.

- `$("If Condition").item.json.payload` (object) **The worker does not return it: reads as undefined.**
- `$("If Condition").item.json.order_id` (string) = `"ord_123"` (Not suggested by the editor)
- `$("If Condition").item.json.amount` (number) = `1250` (Not suggested by the editor)
- `$("If Condition").item.json.status` (string) = `"active"` (Not suggested by the editor)


## How it works

Evaluates a condition and sends the unchanged payload through `true` or `false`.

1. Resolves `expr`: a `{{ }}` is evaluated by the Expression Engine (JavaScript in an isolated context, 100 ms limit) with `$json`, `$("Node")`, `$now` and the other engine variables.
2. If the resolved value is `undefined`, `null` or `""`, returns an error envelope without a port (see errors).
3. A boolean is used as is.
4. A string is evaluated as JSONata against the payload, where fields are referenced directly: `amount > 1000`, `status = "active"`. This also applies to a `{{ }}` that returned a string, and to a `{{ }}` that failed in the engine (its raw text is then parsed as JSONata and fails).
5. Any other type (number, object, array) cannot be compiled as JSONata and goes to `error`.
6. Converts the result with `!!result` and sends the payload through `true` or `false`, with `meta.evaluation` `{ expr, result, raw_result }` (`expr` is the original text).

## In a workflow

### Node

```json
{
  "node_id": "node-high-value",
  "label": "High value?",
  "operator_slug": "flow.if",
  "operator_version": "1.0.0",
  "config": {
    "expr": "{{ $json.amount > 1000 }}",
    "treat_missing_as": false
  }
}
```

### Envelope

```json
{
  "ok": true,
  "data": {
    "order_id": "ord_123",
    "amount": 1250,
    "status": "active"
  },
  "port": "true",
  "meta": {
    "evaluation": {
      "expr": "{{ $json.amount > 1000 }}",
      "result": true,
      "raw_result": true
    }
  }
}
```

## Output ports (behaviour in the code)

### `true`

The condition is truthy.

```json
{
  "order_id": "ord_123",
  "amount": 1250,
  "status": "active"
}
```

### `false`

The condition is falsy, including `undefined` and `null` JSONata results.

```json
{
  "order_id": "ord_124",
  "amount": 80,
  "status": "active"
}
```

### `error`

JSONata compilation or evaluation failed.

```json
{
  "ok": false,
  "errors": [
    "Expression evaluation failed: path.charAt is not a function"
  ],
  "port": "error"
}
```

## Behaviour in the code

- `expr`: Without `{{ }}`, `$json` is not bound in JSONata: `$json.amount > 1000` is always false. A `{{ }}` returning a string such as `{{ $json.name }}` is evaluated again as a JSONata path and is usually false.
- `treat_missing_as`: Has no visible effect: `undefined` and `null` results always go to `false`, and nothing is thrown either way.

## Errors

| Message | Port | Cause and fix |
| --- | --- | --- |
| `Missing configuration: expr is required.` | `out` | `expr` is empty or its `{{ }}` returned `undefined`, `null` or `""` (for example `{{ $json.flag }}` on a missing field). The envelope has no port, so the engine routes it to `out`, which this node does not have: the branch stops silently. Compare explicitly, for example `{{ $json.flag === true }}`. |
| `Expression evaluation failed: path.charAt is not a function` | `error` | The `{{ }}` returned a number, object or array. Return a boolean, for example `{{ $json.items.length > 0 }}`. |
| `Expression evaluation failed: <JSONata message>` | `error` | Invalid JSONata, or a `{{ }}` with a JavaScript error whose raw text was then parsed as JSONata (for example `Expected ":", got "}"`). |

## Notes and limits

- `meta` is recorded in the run steps only: downstream nodes receive `data` as their payload and cannot read `meta` through `$json` or `$("Node")`.
- When the envelope has no `data` (or `data` is falsy), the engine stores the whole envelope as the node output: on `error` the next node receives `{ ok: false, errors: [...], port: "error" }` as its payload.


## Contracts (JSON Schema)

### All (`*.definition.js`)

```json
{
  "slug": "flow.if",
  "name": "If Condition",
  "category": "flow",
  "description": "Evaluates a boolean condition against the input payload to bifurcate the execution flow.\n\n  This operator functions as a gateway that routes execution to either the 'true' or 'false' port based on the result of the configured expression. It supports two evaluation modes: the Sommatic AI Expression Engine ({{ }} syntax, full JavaScript via vm.Script) as the primary engine, and raw JSONata as a simpler fallback for expressions entered without {{ }}.\n\n  **Behavior:**\n  - If the expression evaluates to a truthy value, execution proceeds through the 'true' port.\n  - If the expression evaluates to a falsy value, execution proceeds through the 'false' port.\n  - If 'treat_missing_as' is set to true and the expression yields undefined/null, it is treated as false.",
  "capabilities": [
    "branching",
    "logic",
    "expression-engine",
    "jsonata"
  ],
  "config_schema": {
    "type": "object",
    "required": [
      "expr"
    ],
    "properties": {
      "expr": {
        "type": "string",
        "title": "Condition Expression",
        "description": "An expression that must evaluate to a boolean. Wrap it in {{ }} to use the Sommatic AI Expression Engine (JavaScript) with variables like $json, $input or $(\"Node Name\"), e.g. {{ $json.age >= 18 }} or {{ $json.status === \"active\" }}. Without {{ }} the text is evaluated as JSONata against the payload, where fields are referenced directly: age >= 18 or status = \"active\". $json is not bound in JSONata, so $json.age >= 18 without {{ }} always evaluates to false.",
        "format": "expression"
      },
      "treat_missing_as": {
        "type": "boolean",
        "title": "Treat Missing as False",
        "description": "If enabled, undefined or null results from the expression will automatically route to the False port instead of throwing an error.",
        "default": false
      },
      "execution": {
        "type": "object",
        "title": "Execution Settings",
        "properties": {
          "alwaysOutputData": {
            "type": "boolean",
            "title": "Always Output Data",
            "description": "Always output data, even on error (if configured).",
            "default": false
          },
          "retryOnFail": {
            "type": "boolean",
            "title": "Retry On Fail",
            "description": "Retry the execution if it fails.",
            "default": false
          },
          "maxTries": {
            "type": "integer",
            "title": "Max Retries",
            "description": "Number of retries.",
            "default": 3,
            "displayOptions": {
              "show": {
                "retryOnFail": [
                  true
                ]
              }
            }
          },
          "waitBetweenTries": {
            "type": "integer",
            "title": "Wait Between Retries (ms)",
            "default": 1000,
            "displayOptions": {
              "show": {
                "retryOnFail": [
                  true
                ]
              }
            }
          },
          "continueOnFail": {
            "type": "boolean",
            "title": "Continue On Fail",
            "description": "Do not crash workflow on error.",
            "default": false
          },
          "timeout_ms": {
            "type": "integer",
            "title": "Timeout (ms)",
            "description": "Max execution time for this node before it is killed, in milliseconds. Honored by long-running operators (e.g. OCR/extraction, inline code). Default 300000 (5 min).",
            "default": 300000
          }
        }
      }
    }
  },
  "input_contract_schema": {
    "type": "object",
    "description": "The JSON payload to be evaluated by the expression.",
    "properties": {
      "payload": {
        "type": "object"
      }
    }
  },
  "output_contract_schema": {
    "type": "object",
    "description": "The original payload is passed through to the selected output port.",
    "properties": {
      "payload": {
        "type": "object"
      }
    }
  },
  "ports": [
    {
      "port_id": "true",
      "label": "True",
      "is_default": true,
      "supports_condition": true,
      "description": "Active when expression is true."
    },
    {
      "port_id": "false",
      "label": "False",
      "supports_condition": true,
      "description": "Active when expression is false."
    },
    {
      "port_id": "error",
      "label": "Error",
      "description": "Active when expression evaluation fails."
    }
  ],
  "execution_mode": "sync",
  "tags": [
    "logic",
    "condition",
    "branch",
    "control-flow"
  ],
  "publisher": {
    "identity": "org_sommatic_ai",
    "profile": {
      "name": "Sommatic AI",
      "verified": true,
      "url": "https://me.veripass.com/sommatic",
      "logo_url": null
    }
  }
}
```

### config_schema

```json
{
  "type": "object",
  "required": [
    "expr"
  ],
  "properties": {
    "expr": {
      "type": "string",
      "title": "Condition Expression",
      "description": "An expression that must evaluate to a boolean. Wrap it in {{ }} to use the Sommatic AI Expression Engine (JavaScript) with variables like $json, $input or $(\"Node Name\"), e.g. {{ $json.age >= 18 }} or {{ $json.status === \"active\" }}. Without {{ }} the text is evaluated as JSONata against the payload, where fields are referenced directly: age >= 18 or status = \"active\". $json is not bound in JSONata, so $json.age >= 18 without {{ }} always evaluates to false.",
      "format": "expression"
    },
    "treat_missing_as": {
      "type": "boolean",
      "title": "Treat Missing as False",
      "description": "If enabled, undefined or null results from the expression will automatically route to the False port instead of throwing an error.",
      "default": false
    },
    "execution": {
      "type": "object",
      "title": "Execution Settings",
      "properties": {
        "alwaysOutputData": {
          "type": "boolean",
          "title": "Always Output Data",
          "description": "Always output data, even on error (if configured).",
          "default": false
        },
        "retryOnFail": {
          "type": "boolean",
          "title": "Retry On Fail",
          "description": "Retry the execution if it fails.",
          "default": false
        },
        "maxTries": {
          "type": "integer",
          "title": "Max Retries",
          "description": "Number of retries.",
          "default": 3,
          "displayOptions": {
            "show": {
              "retryOnFail": [
                true
              ]
            }
          }
        },
        "waitBetweenTries": {
          "type": "integer",
          "title": "Wait Between Retries (ms)",
          "default": 1000,
          "displayOptions": {
            "show": {
              "retryOnFail": [
                true
              ]
            }
          }
        },
        "continueOnFail": {
          "type": "boolean",
          "title": "Continue On Fail",
          "description": "Do not crash workflow on error.",
          "default": false
        },
        "timeout_ms": {
          "type": "integer",
          "title": "Timeout (ms)",
          "description": "Max execution time for this node before it is killed, in milliseconds. Honored by long-running operators (e.g. OCR/extraction, inline code). Default 300000 (5 min).",
          "default": 300000
        }
      }
    }
  }
}
```

### input_contract_schema

```json
{
  "type": "object",
  "description": "The JSON payload to be evaluated by the expression.",
  "properties": {
    "payload": {
      "type": "object"
    }
  }
}
```

### output_contract_schema

```json
{
  "type": "object",
  "description": "The original payload is passed through to the selected output port.",
  "properties": {
    "payload": {
      "type": "object"
    }
  }
}
```

### ports

```json
[
  {
    "port_id": "true",
    "label": "True",
    "is_default": true,
    "supports_condition": true,
    "description": "Active when expression is true."
  },
  {
    "port_id": "false",
    "label": "False",
    "supports_condition": true,
    "description": "Active when expression is false."
  },
  {
    "port_id": "error",
    "label": "Error",
    "description": "Active when expression evaluation fails."
  }
]
```
