Skip to content

Built in predicates

Esta Nagy edited this page Jul 24, 2020 · 6 revisions

Here you can find all Predicates defined as part of the standard package.

How to use this document...

Predicates have a single purpose of evaluating input values and returning boolean values. This might or might not need any additional configuration as you will see form our examples.

Each predicate has a version when it was added to the app, a class implementing them, a name they are registered with, an input type, a parameter list defining how they can be configured, a description stating what they are supposed to do, and we will show an example configuration as well.

Predicate implementations

All Match predicate

Name: allMatch

Class: AllMatchPredicate

Since version: 1.1.0

Input type: Object

Parameters

Name Type Description
from Collection < Predicate < Object > > The collection of Predicates we need to evaluate one by one to find the return value.

Description

This predicate returns true if all of the Predicates from the "from" collection returned true, false otherwise.

Example configuration

Applying this configuration

actions:
  - name: "demo"
    rules:
      - name: "deleteFrom"
        path: "$.accounts[*]"
        params:
          keepKey: # this is our predicate
            name: "allMatch"
            from:
              - name: "notNull"
              - name: "regex"
                pattern: ^[a-zA-Z0-9]+$
              - name: "regex"
                pattern: ^.{3,10}$

On this input

{
  "accounts": [
    {
      "ID": 1,
      "fullName": "John Doe",
      "email": "[email protected]",
      "01": true
    },
    {
      "ID": 2,
      "fullName": "Jane Doe",
      "email": "[email protected]",
      "02": true
    }
  ]
}

Produces this output

{
  "accounts": [
    {
      "fullName": "John Doe",
      "email": "[email protected]"
    },
    {
      "fullName": "Jane Doe",
      "email": "[email protected]"
    }
  ]
}

As seen in the example, this predicate matched String keys that are not null, have only alpha-numeric characters and are minimum 3, maximum 10 characters long. The matching keys were kept intact while the others were removed.

Any Match predicate

Name: anyMatch

Class: AnyMatchPredicate

Since version: 1.1.0

Input type: Object

Parameters

Name Type Description
from Collection < Predicate < Object > > The collection of Predicates we need to evaluate one by one to find the return value.

Description

This predicate returns true if any of the Predicates from the "from" collection returned true, false otherwise.

Example configuration

Applying this configuration

actions:
  - name: "demo"
    rules:
      - name: "deleteFrom"
        path: "$.accounts[*]"
        params:
          keepKey: # this is our predicate
            name: "anyMatch"
            from:
              - name: "regex"
                pattern: ^[0-9]+$
              - name: "regex"
                pattern: ^[a-z]+$
              - name: "regex"
                pattern: ^[A-Z]+$

On this input

{
  "accounts": [
    {
      "ID": 1,
      "fullName": "John Doe",
      "email": "[email protected]",
      "01": true
    },
    {
      "ID": 2,
      "fullName": "Jane Doe",
      "email": "[email protected]",
      "02": true
    }
  ]
}

Produces this output

{
  "accounts": [
    {
      "ID": 1,
      "email": "[email protected]",
      "01": true
    },
    {
      "ID": 2,
      "email": "[email protected]",
      "02": true
    }
  ]
}

As seen in the example, this predicate matched String keys that are using either only numeric or only lower case alpha or only upper case alpha characters so these were kept intact while the fields with mixed names were removed.

Any String predicate

Name: anyString

Class: AnyStringPredicate

Since version: 1.0.0

Input type: String

Parameters: < NONE >

Description

This predicate returns true for any String.

Example configuration

Applying this configuration

actions:
  - name: "demo"
    rules:
      - name: "replace"
        path: "$.accounts..expiresOn"
        params:
          stringFunction: # this is the node defining our function
            name: "stringDateAdd"
            formatter: "yyyy-MM-dd"
            amount: 6
            unit: "MONTHS"
          predicate: # we want to match any String
            name: "anyString"

On this input

{
  "accounts": [
    {
      "id": 1,
      "name": "John Doe",
      "expiresOn": "2020-03-14"
    },
    {
      "id": 2,
      "name": "Jane Doe"
    }
  ]
}

Produces this output

{
  "accounts": [
    {
      "id": 1,
      "name": "John Doe",
      "expiresOn": "2020-09-14"
    },
    {
      "id": 2,
      "name": "Jane Doe"
    }
  ]
}

In this example the date manipulation function was not restricted as the any String predicate returned true for all input Strings.

Contains key predicate

Name: containsKey

Class: ContainsKeyPredicate

Since version: 1.1.0

Input type: Map < String , Object >

Parameters

Name Type Description
key String The name of the key we want to check.

Description

This predicate observes the input as a Map and returns true if it has a key named exactly matching the value of the key.

Example configuration

Applying this configuration

actions:
  - name: "demo"
    rules:
      - name: "deleteFrom"
        path: "$.accounts[*]"
        params:
          predicate:
            name: "containsKey"
            key: "address"
          keepKey:
            name: "regex"
            pattern: ^[a-z]+$
          deleteKey:
            name: "regex"
            pattern: ^password$

On this input

{
  "accounts": [
    {
      "address": {
        "home": "123 Main street, SomeCity"
      },
      "id": 1,
      "name": "John Doe",
      "password": "21fe12e12ef7f8a123db123",
      "totalComments": 0
    },
    {
      "id": 2,
      "name": "Jane Doe",
      "totalComments": 10
    }
  ]
}

Produces this output

{
  "accounts": [
    {
      "address": {
        "home": "123 Main street, SomeCity"
      },
      "id": 1,
      "name": "John Doe"
    },
    {
      "id": 2,
      "name": "Jane Doe",
      "totalComments": 10
    }
  ]
}

In this example we have used the contains key predicate to allow the rule execution only in case the account has an address field.

Eval on predicate

Name: evalOn

Class: EvalOnPredicate

Since version: 1.1.0

Input type: Map < String , Object >

Parameters

Name Type Description
childPath String The desired navigation we want to perform relative to the Map/Object we operate on.
predicate Predicate < Object > The Predicate we need to evaluate after the navigation is done.

Description

This predicate attempts to convert the input to a Map representation and attempts to navigate using the keys of the Map as it is defined by the childPath parameter. The childPath parameter is split using a single dot '.' as delimiter to find the small steps we need to take one after the other. Once the navigation is done and reaches a valid path, we apply the predicate.

Example configuration

Applying this configuration

actions:
  - name: "demo"
    rules:
      - name: "deleteFrom"
        path: "$.accounts[*]"
        params:
          predicate: # this is our predicate
            name: "evalOn"
            predicate:
              name: "regex"
              pattern: ^[0-9]+$
            childPath: "address.billing.zipCode"
          deleteKey:
            name: "regex"
            pattern: ^address$

On this input

{
  "accounts": [
    {
      "id": 1,
      "name": "John Doe",
      "address": {
        "shipping": {
          "city": "Some City",
          "zipCode": "1300",
          "address": "Some street 123."
        },
        "billing": {
          "city": "Some City",
          "zipCode": "1300",
          "address": "Some street 123."
        }
      }
    },
    {
      "id": 2,
      "name": "Jane Doe",
      "address": {
        "shipping": {
          "city": "Some City",
          "zipCode": "1300",
          "address": "Some street 123."
        }
      }
    }
  ]
}

Produces this output

{
  "accounts": [
    {
      "id": 1,
      "name": "John Doe"
    },
    {
      "id": 2,
      "name": "Jane Doe",
      "address": {
        "shipping": {
          "city": "Some City",
          "zipCode": "1300",
          "address": "Some street 123."
        }
      }
    }
  ]
}

In this example we have used the eval on predicate to navigate to the "zipCode" of the "billing" "address" and check whether it is numerical value to decide about the removal of the "address" key on the account level.

Is null predicate

Name: isNull

Class: IsNullPredicate

Since version: 1.1.0

Input type: Object

Parameters: < NONE >

Description

This predicate returns true for null.

Example configuration

Applying this configuration

actions:
  - name: "demo"
    rules:
      - name: "deleteFrom"
        path: "$.accounts[*]"
        params:
          predicate:
            name: "containsKey"
            key: "address"
          keepKey: # keep fields which have null names
            name: "isNull"

On this input

{
  "accounts": [
    {
      "address": {
        "home": "123 Main street, SomeCity"
      },
      "id": 1,
      "name": "John Doe",
      "password": "21fe12e12ef7f8a123db123",
      "totalComments": 0
    },
    {
      "id": 2,
      "name": "Jane Doe",
      "totalComments": 10
    }
  ]
}

Produces this output

{
  "accounts": [
    {
    },
    {
      "id": 2,
      "name": "Jane Doe",
      "totalComments": 10
    }
  ]
}

In this example all of the keys were removed because none of them were null.

None Match predicate

Name: noneMatch

Class: NoneMatchPredicate

Since version: 1.1.0

Input type: Object

Parameters

Name Type Description
from Collection < Predicate < Object > > The collection of Predicates we need to evaluate one by one to find the return value.

Description

This predicate returns true if none of the Predicates from the "from" collection returned true, false otherwise.

Example configuration

Applying this configuration

actions:
  - name: "demo"
    rules:
      - name: "deleteFrom"
        path: "$.accounts[*]"
        params:
          keepKey: # this is our predicate
            name: "noneMatch"
            from:
              - name: "regex"
                pattern: ^[0-9]+$
              - name: "regex"
                pattern: ^[a-z]+$
              - name: "regex"
                pattern: ^[A-Z]+$

On this input

{
  "accounts": [
    {
      "ID": 1,
      "fullName": "John Doe",
      "email": "[email protected]",
      "01": true
    },
    {
      "ID": 2,
      "fullName": "Jane Doe",
      "email": "[email protected]",
      "02": true
    }
  ]
}

Produces this output

{
  "accounts": [
    {
      "fullName": "John Doe"
    },
    {
      "fullName": "Jane Doe"
    }
  ]
}

As seen in the example, this predicate matched if the key was neither only lower case alpha nor only upper case alpha nor only numeric. Therefore only "fullName" was kept while every other key was removed.

Not null predicate

Name: notNull

Class: NotNullPredicate

Since version: 1.1.0

Input type: Object

Parameters: < NONE >

Description

This predicate returns true for anything that is not null.

Example configuration

Applying this configuration

actions:
  - name: "demo"
    rules:
      - name: "deleteFrom"
        path: "$.accounts[*]"
        params:
          predicate:
            name: "notNull"
          keepKey: # keep fields which have non-null names
            name: "notNull"

On this input

{
  "accounts": [
    {
      "address": {
        "home": "123 Main street, SomeCity"
      },
      "id": 1,
      "name": "John Doe",
      "password": "21fe12e12ef7f8a123db123",
      "totalComments": 0
    },
    {
      "id": 2,
      "name": "Jane Doe",
      "totalComments": 10
    }
  ]
}

Produces this output

{
  "accounts": [
    {
      "address": {
        "home": "123 Main street, SomeCity"
      },
      "id": 1,
      "name": "John Doe",
      "password": "21fe12e12ef7f8a123db123",
      "totalComments": 0
    },
    {
      "id": 2,
      "name": "Jane Doe",
      "totalComments": 10
    }
  ]
}

In this example all of the keys were kept because none of them were null.

RegEx predicate

Name: regex

Class: RegexPredicate

Since version: 1.1.0

Input type: String

Parameters

Name Type Description
pattern String The regular expression we want to match.

Description

This predicate returns true if input value matches the regular expression defined in the "pattern" parameter.

Example configuration

Applying this configuration

actions:
  - name: "demo"
    rules:
      - name: "deleteFrom"
        path: "$.accounts[*]"
        params:
          predicate:
            name: "containsKey"
            key: "address"
          keepKey:
            name: "regex"
            pattern: ^[a-z]+$
          deleteKey:
            name: "regex"
            pattern: ^password$

On this input

{
  "accounts": [
    {
      "address": {
        "home": "123 Main street, SomeCity"
      },
      "id": 1,
      "name": "John Doe",
      "password": "21fe12e12ef7f8a123db123",
      "totalComments": 0
    },
    {
      "id": 2,
      "name": "Jane Doe",
      "totalComments": 10
    }
  ]
}

Produces this output

{
  "accounts": [
    {
      "address": {
        "home": "123 Main street, SomeCity"
      },
      "id": 1,
      "name": "John Doe"
    },
    {
      "id": 2,
      "name": "Jane Doe",
      "totalComments": 10
    }
  ]
}

This example shown 2 RegEx predicates right away. One told the rule to keep lower case alpha keys, while the other selected the password field for removal.

SpEL predicate

Name: SpEL

Class: SpringExpressionLanguagePredicate

Since version: 1.1.0

Input type: Object

Parameters

Name Type Description
expression String The SpEL expression we want to match.

Description

This predicate returns true if input value matches the SpEL expression defined in the "expression" parameter.

Example configuration

Applying this configuration

actions:
  - name: "demo"
    rules:
      - name: "calculate"
        path: "$.posts[*].likes"
        params:
          predicate: # this is the node defining the predicate
            name: "SpEL"
            expression: "(#root instanceof T(java.math.BigDecimal)) && #root.longValue() > 3"
          numberFunction: # this is the node defining the numerical calculation
            name: "subtract"
            operand: 3
            scale: 0

On this input

{
  "posts": [
    {
      "id": 1,
      "title": "Post title 1",
      "likes": 30,
      "views": 20
    },
    {
      "id": 2,
      "title": "Post title 2",
      "likes": 0,
      "views": 5
    }
  ]
}

Produces this output

{
  "posts": [
    {
      "id": 1,
      "title": "Post title 1",
      "likes": 27,
      "views": 20
    },
    {
      "id": 2,
      "title": "Post title 2",
      "likes": 0,
      "views": 5
    }
  ]
}

The example shows how this predicate help us to exclude the node where the reduction of 3 would have resulted in an invalid value considering that negative number of likes is not possible.