Skip to content

Instantly share code, notes, and snippets.

@JWally
Created December 29, 2023 22:54
Show Gist options
  • Save JWally/a9d01cab48f92298d3a7082cc074909a to your computer and use it in GitHub Desktop.
Save JWally/a9d01cab48f92298d3a7082cc074909a to your computer and use it in GitHub Desktop.

The Rules Engine

Introducing "The Rules Engine" – the pinnacle of precision and flexibility in digital security and user experience management.

Designed to empower businesses with the ability to construct meticulously detailed rules, this engine stands as a bulwark against fraud and account takeover attempts. With its advanced logic capabilities, "The Rules Engine" enables you to define conditions with surgical precision, ensuring that each user interaction is scrutinized for potential threats, while simultaneously maintaining a seamless and frictionless experience for your seasoned users.

It's not just a tool; it's your frontline defense and smart facilitator, adeptly balancing security and user convenience. Whether it's safeguarding sensitive information or streamlining user access, "The Rules Engine" adapts to the complex and evolving landscape of digital interactions, making it an indispensable ally in the modern digital era.

Logic Operators

$and

Explicitly define a set of conditions that must all be true. Use an array to list these conditions.

  • Example: To check if a user is over 18 and active:
    {
      "$and": [
        { "age": { "$gte": 18 } },
        { "status": "active" }
      ]
    }

$or

Define a set of conditions where at least one must be true. Like $and, it uses an array.

  • Example: To check if a user is either over 18 or has a "veteran" status:
    {
      "$or": [
        { "age": { "$gte": 18 } },
        { "status": "veteran" }
      ]
    }

Comparison Operators

$gte (Greater Than or Equal)

Checks if the value is greater than or equal to the specified number.

  • Example: {"age": {"$gte": 18}} - true if age is 18 or more.

$gt (Greater Than)

Checks if the value is strictly greater than the specified number.

  • Example: {"age": {"$gt": 18}} - true if age is more than 18.

$lte (Less Than or Equal)

Checks if the value is less than or equal to the specified number.

  • Example: {"age": {"$lte": 18}} - true if age is 18 or less.

$lt (Less Than)

Checks if the value is strictly less than the specified number.

  • Example: {"age": {"$lt": 18}} - true if age is less than 18.

$eq (Equal)

Checks if the value is equal to the specified value.

  • Example: {"name": {"$eq": "John"}} - true if name is "John".
  • Example: {"active": {"$eq": true}}

$ne (Not Equal)

Checks if the value is not equal to the specified value.

  • Example: {"name": {"$ne": "John"}} - true if name is not "John".

$in (In Array)

Checks if the value is in the specified array.

  • Example: {"color": {"$in": ["red", "blue"]}} - true if color is either "red" or "blue".

$nin (Not In Array)

Checks if the value is not in the specified array.

  • Example: {"color": {"$nin": ["red", "blue"]}} - true if color is neither "red" nor "blue".

$not

Applies a logical NOT to the specified condition.

  • Example: {"status": {"$not": "inactive"}} - true if status is not "inactive".

Dynamic Key Evaluation

This feature allows using the values of the data object as keys in the logic object.

Say you want the user's CURRENT country to have been used at least 25% of the time by the user. Since you can't know the user's country of origin in first, you can craft a rule like this:

  • Example:
    {
      "userModel.country_code.${ipGeoData.country_code}.percent": { 
          "$lt": 0.25
      }
    }
    Here, because ipGeoData.country_code is say "US", the data-object you're looking up becomes userModel.country_code.US.percent, which is what you want.

Mathematical Operations

$sum

Calculates the sum of specified values in an array.

  • Example:
"conditions": {
    "$and": [
        {
            "2500": {
                "$gte": {
                    "$sum": [
                        "userModel.metadata.amount._sum_24_hr_",
                        "eventMetadata.amount"
                    ]
                }
            }
        },
    ]
},

Here, we're saying if the sum of money spent in the last 24 hours AND the current transaction amount are Greater Than or Equal to; evaluate to true

Other Arithmetic Operations

  • $add: Adds two values.
  • $subtract: Subtracts the second value from the first.
  • $multiply: Multiplies two values.
  • $divide: Divides the first value by the second.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment