jsonlogic icon indicating copy to clipboard operation
jsonlogic copied to clipboard

[Feature Request] Array comparison operators (contains_all, contains_any, contains_none)

Open iyk2h opened this issue 1 month ago • 0 comments

Summary

I'd like to propose adding array comparison operators that are commonly needed in workflow automation systems.

Proposed Operators

Operator Description Example
contains_all Check if all elements in B exist in A {"contains_all": [["a","b","c"], ["a","b"]]} → true
contains_any Check if any element in B exists in A {"contains_any": [["a","b"], ["b","c"]]} → true
contains_none Check if no elements in B exist in A {"contains_none": [["a","b"], ["c","d"]]} → true

Use Case

I'm building a workflow automation system where users define conditions in the UI, such as:

  • "If selected options contain all of ['VIP', 'Premium'] → send to Slack"
  • "If tags contain any of ['urgent', 'important'] → notify immediately"
  • "If categories contain none of ['blocked', 'spam'] → proceed"

The current in operator only checks if a single value exists in an array. I need array-to-array comparison.

Implementation

I've already implemented these operators in production and can submit a PR:

jsonlogic.AddOperator("contains_all", func(values, data any) any {
    valuesSlice, ok := values.([]interface{})
    if !ok || len(valuesSlice) != 2 {
        return false
    }

    searchArray := valuesSlice[0].([]interface{})
    requiredArray := valuesSlice[1].([]interface{})

    for _, required := range requiredArray {
        found := false
        for _, item := range searchArray {
            if reflect.DeepEqual(item, required) {
                found = true
                break
            }
        }
        if !found {
            return false
        }
    }
    return true
})

Questions

  1. Would you accept a PR for these operators?
  2. Any preferences on naming convention?
  3. Should I also include string operators (begins_with, ends_with, string_contains) in a separate PR?

Happy to contribute! Looking forward to your feedback.

iyk2h avatar Nov 30 '25 11:11 iyk2h