jsonlogic
jsonlogic copied to clipboard
[Feature Request] Array comparison operators (contains_all, contains_any, contains_none)
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
- Would you accept a PR for these operators?
- Any preferences on naming convention?
- Should I also include string operators (begins_with, ends_with, string_contains) in a separate PR?
Happy to contribute! Looking forward to your feedback.