Skip to content

Implementing your own predicates

Esta Nagy edited this page Jul 1, 2020 · 3 revisions

Predicates are the ultimate decision-makers. They can test whatever logical expression you configure them to test based on the input you throw at them.

Please check out the following list of simple steps to implement a new predicate:

  1. Create a class for your predicate implementing the Predicate interface using Object as type parameter
  2. Create a constructor using only the supported parameter types as parameters
  3. Annotate your constructor with @NamedPredicate
  4. Annotate all of your constructor parameters as defined here
  5. Implement the test(Object) method defined by the interface. The implementation should make some type checks as conversion is not always possible despite the best of our intentions.
  6. Register your predicate using FunctionRegistry#registerPredicateClass(Class)
  7. Make sure to add your predicate to the #/definitions/predicateTypes/definitions node of our JSON Schema
    1. Add a new definition similarly to the examples already in there
    2. Register the type predicate to the definition of the anyPredicate definition by
      1. Adding the name to the enum containing all the names
      2. Adding a new object to the list similar to this:
          {
            "if": {
              "properties": {
                "name": {
                  "const": "allMatch"
                }
              }
            },
            "then": {
              "$ref": "#/definitions/predicateTypes/definitions/predicateAllMatch"
            }
          }
  8. Let's put it to the test (pun intended)!