SwiftRuleEngine

1.4.1

A rule engine written in Swift where rules are defined in JSON format
santalvarez/swift-rule-engine

What's New

1.4.1

2024-05-07T00:40:26Z
  • Fixed bug in Not* operators that resulted in the operators not matching when they should.

swift-rule-engine

A rule engine written in Swift where rules are defined in JSON format.

Installation

CocoaPods

pod 'SwiftRuleEngine'

Swift Package Manager

dependencies: [
    .package(url: "https://github.com/santalvarez/swift-rule-engine.git",
             from: upToNextMajor("1.4.0"))
]

Quick Example

import SwiftRuleEngine

let rule: [String: Any] = [
    "name": "test-rule",
    "description": "Test rule",
    "conditions": [
        "all": [
            [
                "path": "$.player.first_name",
                "value": "Lionel",
                "operator": "equal"
            ],
            [
                "path": "$.player.last_name",
                "value": "Messi",
                "operator": "equal"
            ]
        ]
    ]
]

let obj = [
    "player": [
        "first_name": "Lionel",
        "last_name": "Messi"
    ]
]

let engine = try RuleEngine(rules: [rule])

let result = engine.evaluate(obj)

Advanced Example

import SwiftRuleEngine

let rule: [String: Any] = [
    "name": "advanced-rule",
    "extra": ["author": "Santiago Alvarez"],
    "conditions": [
        "all": [
            [
                "path": "$.player.first_name",
                "value": "Lionel",
                "operator": "equal"
            ],
            [
                "path": "$.player.clubs",
                "operator": "contains"
                "value": ["Boca Juniors", "Juventus", "PSG"],
                "params": ["mode": "any"]
            ],
            [
              "path": "$.player.last_name",
              "operator": "in",
              "value": ["Messi", "Ronaldo", "Neymar"]
            ],
            [
              "path": "$.player.age",
              "operator": "less_than",
              "value": 35
            ],
            [
              "path": "$.player.clubs[0]",
              "operator": "equal",
              "value": "Barcelona"
            ]
        ]
    ]
]

let obj = [
    "player": [
        "first_name": "Lionel",
        "last_name": "Messi",
        "age": 34,
        "goals": 700,
        "clubs": [
            "Barcelona",
            "PSG",
            "InterMiami"
        ]
    ]
]

let engine = try RuleEngine(rules: [rule])

let result = engine.evaluate(obj)

Description

  • Swift Tools 5.8.0
View More Packages from this Author

Dependencies

  • None
Last updated: Sat Nov 23 2024 16:13:51 GMT-1000 (Hawaii-Aleutian Standard Time)