Condition

public struct Condition : Equatable

A structure that defines conditional logic for determining whether data in your application meets specific criteria. Conditions are the building blocks used by Rules to decide when to trigger certain actions, such as dispatching events, collecting or transforming data.

## Overview

A Condition evaluates data from your application’s data layer against specified criteria. For example, you might create a condition to check if a user’s subscription status equals “premium”, or if a purchase amount is greater than $100.

## Basic Usage

For creating Condition instances it’s recommended to use the convenience static methods provided in the Condition extension:

 // Check if user type equals "premium"
 let condition = Condition.equals(ignoreCase: false, 
                                  variable: "user_type", // for a flat key
                                  target: "premium")

 // Check if purchase amount is greater than 100
 let condition = Condition.isGreaterThan(orEqual: false,
                                         variable: JSONPath["order"]["purchase_amount"], // for a JSON path
                                         number: "100")

## Error Handling

When conditions are evaluated, they may throw ConditionEvaluationError in exceptional cases:

  • If the specified variable doesn’t exist in the data (except for isDefined/isNotDefined operators)
  • If a required filter value is missing
  • If numeric operations are attempted on non-numeric data
  • If an operator is not supported for the data type found

## Supported Data Types

Conditions can evaluate various data types including strings, numbers, booleans, arrays, and dictionaries. The behavior varies by operator - for example, string operations will convert other types to strings, while numeric operations require parseable numbers.

  • The operator used to evaluate a condition against a variable.

    See more

    Declaration

    Swift

    public enum Operator : Equatable
  • Returns a Condition that checks whether the value found at path variable is equal to the given target.

    Declaration

    Swift

    static func equals(ignoreCase: Bool, variable: JSONObjectPath, target: String) -> Condition

    Parameters

    ignoreCase

    true if the equality check should be done in a case-insensitive way; else false

    variable

    the path to the variable in the payload to extract the value from for the comparison

    target

    the target value to check against

  • Returns a Condition that checks whether the value found at key variable is equal to the given target.

    Declaration

    Swift

    static func equals(ignoreCase: Bool, variable: String, target: String) -> Condition

    Parameters

    ignoreCase

    true if the equality check should be done in a case-insensitive way; else false

    variable

    the key to the variable in the payload to extract the value from for the comparison

    target

    the target value to check against

  • Returns a Condition that checks whether the value found at path variable is not equal to the given target.

    Declaration

    Swift

    static func doesNotEqual(ignoreCase: Bool, variable: JSONObjectPath, target: String) -> Condition

    Parameters

    ignoreCase

    true if the equality check should be done in a case-insensitive way; else false

    variable

    the path to the variable in the payload to extract the value from for the comparison

    target

    the target value to check against

  • Returns a Condition that checks whether the value found at key variable is not equal to the given target.

    Declaration

    Swift

    static func doesNotEqual(ignoreCase: Bool, variable: String, target: String) -> Condition

    Parameters

    ignoreCase

    true if the equality check should be done in a case-insensitive way; else false

    variable

    the key to the variable in the payload to extract the value from for the comparison

    target

    the target value to check against

  • Returns a Condition that checks whether the value found at path variable, as a string, contains the string within it.

    Declaration

    Swift

    static func contains(ignoreCase: Bool, variable: JSONObjectPath, string: String) -> Condition

    Parameters

    ignoreCase

    true if the comparison should be done in a case-insensitive way; else false

    variable

    the path to the variable in the payload to extract the value from for the comparison

    string

    the target value to check against

  • Returns a Condition that checks whether the value found at key variable, as a string, contains the string within it.

    Declaration

    Swift

    static func contains(ignoreCase: Bool, variable: String, string: String) -> Condition

    Parameters

    ignoreCase

    true if the comparison should be done in a case-insensitive way; else false

    variable

    the key to the variable in the payload to extract the value from for the comparison

    string

    the target value to check against

  • Returns a Condition that checks whether the value found at path variable, as a string, does not contain the string within it.

    Declaration

    Swift

    static func doesNotContain(ignoreCase: Bool, variable: JSONObjectPath, string: String) -> Condition

    Parameters

    ignoreCase

    true if the comparison should be done in a case-insensitive way; else false

    variable

    the path to the variable in the payload to extract the value from for the comparison

    string

    the target value to check against

  • Returns a Condition that checks whether the value found at key variable, as a string, does not contain the string within it.

    Declaration

    Swift

    static func doesNotContain(ignoreCase: Bool, variable: String, string: String) -> Condition

    Parameters

    ignoreCase

    true if the comparison should be done in a case-insensitive way; else false

    variable

    the key to the variable in the payload to extract the value from for the comparison

    string

    the target value to check against

  • Returns a Condition that checks whether the value found at path variable, as a string, starts with the given prefix.

    Declaration

    Swift

    static func startsWith(ignoreCase: Bool, variable: JSONObjectPath, prefix: String) -> Condition

    Parameters

    ignoreCase

    true if the comparison should be done in a case-insensitive way; else false

    variable

    the path to the variable in the payload to extract the value from for the comparison

    prefix

    the target value to check against

  • Returns a Condition that checks whether the value found at key variable, as a string, starts with the given prefix.

    Declaration

    Swift

    static func startsWith(ignoreCase: Bool, variable: String, prefix: String) -> Condition

    Parameters

    ignoreCase

    true if the comparison should be done in a case-insensitive way; else false

    variable

    the key to the variable in the payload to extract the value from for the comparison

    prefix

    the target value to check against

  • Returns a Condition that checks whether the value found at path variable, as a string, does not start with the given prefix.

    Declaration

    Swift

    static func doesNotStartWith(ignoreCase: Bool, variable: JSONObjectPath, prefix: String) -> Condition

    Parameters

    ignoreCase

    true if the comparison should be done in a case-insensitive way; else false

    variable

    the path to the variable in the payload to extract the value from for the comparison

    prefix

    the target value to check against

  • Returns a Condition that checks whether the value found at key variable, as a string, does not start with the given prefix.

    Declaration

    Swift

    static func doesNotStartWith(ignoreCase: Bool, variable: String, prefix: String) -> Condition

    Parameters

    ignoreCase

    true if the comparison should be done in a case-insensitive way; else false

    variable

    the key to the variable in the payload to extract the value from for the comparison

    prefix

    the target value to check against

  • Returns a Condition that checks whether the value found at path variable, as a string, ends with the given suffix.

    Declaration

    Swift

    static func endsWith(ignoreCase: Bool, variable: JSONObjectPath, suffix: String) -> Condition

    Parameters

    ignoreCase

    true if the comparison should be done in a case-insensitive way; else false

    variable

    the path to the variable in the payload to extract the value from for the comparison

    suffix

    the target value to check against

  • Returns a Condition that checks whether the value found at key variable, as a string, ends with the given suffix.

    Declaration

    Swift

    static func endsWith(ignoreCase: Bool, variable: String, suffix: String) -> Condition

    Parameters

    ignoreCase

    true if the comparison should be done in a case-insensitive way; else false

    variable

    the key to the variable in the payload to extract the value from for the comparison

    suffix

    the target value to check against

  • Returns a Condition that checks whether the value found at path variable, as a string, does not end with the given suffix.

    Declaration

    Swift

    static func doesNotEndWith(ignoreCase: Bool, variable: JSONObjectPath, suffix: String) -> Condition

    Parameters

    ignoreCase

    true if the comparison should be done in a case-insensitive way; else false

    variable

    the path to the variable in the payload to extract the value from for the comparison

    suffix

    the target value to check against

  • Returns a Condition that checks whether the value found at key variable, as a string, does not end with the given suffix.

    Declaration

    Swift

    static func doesNotEndWith(ignoreCase: Bool, variable: String, suffix: String) -> Condition

    Parameters

    ignoreCase

    true if the comparison should be done in a case-insensitive way; else false

    variable

    the key to the variable in the payload to extract the value from for the comparison

    suffix

    the target value to check against

  • Returns a Condition that checks whether the value can be found at path variable.

    Declaration

    Swift

    static func isDefined(variable: JSONObjectPath) -> Condition

    Parameters

    variable

    the path to the variable in the payload to extract the value from for the comparison

  • Returns a Condition that checks whether the value can be found at key variable.

    Declaration

    Swift

    static func isDefined(variable: String) -> Condition

    Parameters

    variable

    the key to the variable in the payload to extract the value from for the comparison

  • Returns a Condition that checks whether the value can not be found at path variable.

    Declaration

    Swift

    static func isNotDefined(variable: JSONObjectPath) -> Condition

    Parameters

    variable

    the path to the variable in the payload to extract the value from for the comparison

  • Returns a Condition that checks whether the value can not be found at key variable.

    Declaration

    Swift

    static func isNotDefined(variable: String) -> Condition

    Parameters

    variable

    the key to the variable in the payload to extract the value from for the comparison

  • Returns a Condition that checks whether the value at path variable is considered to be “empty”.

    “Empty” is considered as the following for the different supported input types:

    • String.isEmpty
    • Array.isEmpty
    • Dictionary.isEmpty
    • value == NSNull()

    Numeric values are always considered as not empty.

    Declaration

    Swift

    static func isEmpty(variable: JSONObjectPath) -> Condition

    Parameters

    variable

    the path to the variable in the payload to extract the value from for the comparison

  • Returns a Condition that checks whether the value at key variable is considered to be “empty”.

    “Empty” is considered as the following for the different supported input types:

    • String.isEmpty
    • Array.isEmpty
    • Dictionary.isEmpty
    • value == NSNull()

    Numeric values are always considered as not empty.

    Declaration

    Swift

    static func isEmpty(variable: String) -> Condition

    Parameters

    variable

    the key to the variable in the payload to extract the value from for the comparison

  • Returns a Condition that checks whether the value at path variable is considered to be “not empty”.

    “Not empty” is considered as the following for the different supported input types:

    • !String.isEmpty
    • !Array.isEmpty
    • !Dictionary.isEmpty
    • value != NSNull()

    Numeric values are always considered as not empty.

    Declaration

    Swift

    static func isNotEmpty(variable: JSONObjectPath) -> Condition

    Parameters

    variable

    the path to the variable in the payload to extract the value from for the comparison

  • Returns a Condition that checks whether the value at key variable is considered to be “not empty”.

    “Not empty” is considered as the following for the different supported input types:

    • !String.isEmpty
    • !Array.isEmpty
    • !Dictionary.isEmpty
    • value != NSNull()

    Numeric values are always considered as not empty.

    Declaration

    Swift

    static func isNotEmpty(variable: String) -> Condition

    Parameters

    variable

    the key to the variable in the payload to extract the value from for the comparison

  • Returns a Condition that checks whether the Double value found at path variable, is greater than the Double value given by number

    Declaration

    Swift

    static func isGreaterThan(orEqual: Bool, variable: JSONObjectPath, number: String) -> Condition

    Parameters

    orEqual

    true if numbers can also be equal; else false

    variable

    the path to the variable in the payload to extract the value from for the comparison

    number

    the target value to check against

  • Returns a Condition that checks whether the Double value found at key variable, is greater than the Double value given by number

    Declaration

    Swift

    static func isGreaterThan(orEqual: Bool, variable: String, number: String) -> Condition

    Parameters

    orEqual

    true if numbers can also be equal; else false

    variable

    the key to the variable in the payload to extract the value from for the comparison

    number

    the target value to check against

  • Returns a Condition that checks whether the Double value found at path variable, is less than the Double value given by number

    Declaration

    Swift

    static func isLessThan(orEqual: Bool, variable: JSONObjectPath, number: String) -> Condition

    Parameters

    orEqual

    true if numbers can also be equal; else false

    variable

    the path to the variable in the payload to extract the value from for the comparison

    number

    the target value to check against

  • Returns a Condition that checks whether the Double value found at key variable, is less than the Double value given by number

    Declaration

    Swift

    static func isLessThan(orEqual: Bool, variable: String, number: String) -> Condition

    Parameters

    orEqual

    true if numbers can also be equal; else false

    variable

    the key to the variable in the payload to extract the value from for the comparison

    number

    the target value to check against

  • Returns a Condition that checks whether the value found at path variable, as a string, is matched by the given regex.

    Declaration

    Swift

    static func regularExpression(variable: JSONObjectPath, regex: String) -> Condition

    Parameters

    variable

    the path to the variable in the payload to extract the value from for the comparison

    regex

    the target regex to check against

  • Returns a Condition that checks whether the value found at key variable, as a string, is matched by the given regex.

    Declaration

    Swift

    static func regularExpression(variable: String, regex: String) -> Condition

    Parameters

    variable

    the key to the variable in the payload to extract the value from for the comparison

    regex

    the target regex to check against