Allow processing of additional object types by regex function in Rules
Summary
I had posted a discussion topic about an issue I was running into with creating a regex rule function: https://github.com/StackStorm/st2/discussions/5363
tl;dr - You can't utilize the regex function in a rule to check against list type objects. This means there's no way to check a single list type object for two or more values as Rule criteria is a dictionary which means you can't use contains against the same value twice. When attempting to process the regex, if the payload object is a list, the payload value is dropped.
I asked in the ST2 community slack if converting lists and other set objects to strings, before applying the regex, to give it broader functionality, is something that would be totally out of the question. I received some feedback from @amanda11 that what I was doing might be doable with jsonpath-rw and wasn't an unreasonable feature request if there isn't some other underlying reason for only applying regex to strings and bytes.
I took a look myself at seeing if I could finagle jsonpath-rw functions to do what I wanted in the rule criteria locally, but was unsuccesful so I believe this may require adding some logic for specific processing of lists to operators.py or util/payload.py.
Stackstorm Version
Stackstorm 3.5.0
Steps to reproduce the problem
Create a rule that utilizes a key which references a list object value (e.g [ Item, Item Two, Three Word Item ] and attempt to apply a regex function:
criteria:
trigger.body.list_object:
type: "regex"
pattern: >
^(?=.*Item)(?=.*Three Word Item).*$
Expected results :
(via st2-rule-tester)
2021-09-17 14:46:11,081 DEBUG [-] Rendered criteria pattern: ^(?=.*Item)(?=.*Three Word Item).*$
2021-09-17 14:46:11,090 INFO [-] Validation for rule my_pack.regex_criteria_test succeeded on criteria -
key: trigger.body.list_object
pattern: ^(?=.*Item)(?=.*Three Word Item).*$
type: regex
payload: "[ Item, Item Two, Three Word Item ]"
2021-09-17 13:37:47,914 DEBUG [-] Rule 6143ae921f112167fbeb6676 applicable for 081fdd29-f1d3-4f10-ace8-0b22c9359059.
2021-09-17 13:37:47,914 DEBUG [-] [1st_pass] 1 rule(s) found to enforce for 081fdd29-f1d3-4f10-ace8-0b22c9359059.
2021-09-17 13:37:47,914 INFO [-] 1 rule(s) found to enforce for 081fdd29-f1d3-4f10-ace8-0b22c9359059.
2021-09-17 13:37:47,914 INFO [-] === RULE MATCHES ===
Actual results:
2021-09-17 13:37:47,895 INFO [-] Validating rule my_pack.regex_criteria_test for 081fdd29-f1d3-4f10-ace8-0b22c9359059.
2021-09-17 13:37:47,896 DEBUG [-] Trigger payload:
2021-09-17 13:37:47,901 DEBUG [-] Rendered criteria pattern: ^(?=.*Item)(?=.*Three Word Item).*$
2021-09-17 13:37:47,910 ERROR [-] There might be a problem with the criteria in rule my_pack.regex_criteria_test
Traceback (most recent call last):
File "/opt/stackstorm/st2/lib/python3.6/site-packages/st2reactor/rules/filter.py", line 161, in _check_criterion
result = op_func(value=payload_value, criteria_pattern=criteria_pattern)
File "/opt/stackstorm/st2/lib/python3.6/site-packages/st2common/operators.py", line 265, in regex
return regex.search(value) is not None
TypeError: expected string or bytes-like object
2021-09-17 13:37:47,914 INFO [-] Validation for rule my_pack.regex_criteria_test failed on criteria -
key: trigger.body.list_object
pattern: None
type: regex
payload: None
2021-09-17 13:37:47,914 DEBUG [-] Rule 6143ae921f112167fbeb6676 not applicable for 081fdd29-f1d3-4f10-ace8-0b22c9359059.
2021-09-17 13:37:47,914 DEBUG [-] [1st_pass] 0 rule(s) found to enforce for 081fdd29-f1d3-4f10-ace8-0b22c9359059.
2021-09-17 13:37:47,914 DEBUG [-] [2nd_pass] 0 rule(s) found to enforce for 081fdd29-f1d3-4f10-ace8-0b22c9359059.
2021-09-17 13:37:47,914 INFO [-] 0 rule(s) found to enforce for 081fdd29-f1d3-4f10-ace8-0b22c9359059.
2021-09-17 13:37:47,914 INFO [-] === RULE DOES NOT MATCH ===
Thanks for contributing to this issue. As it has been 90 days since the last activity, we are automatically marking is as stale. If this issue is not relevant or applicable anymore (problem has been fixed in a new version or similar), please close the issue or let us know so we can close it. On the contrary, if the issue is still relevant, there is nothing you need to do, but if you have any additional details or context which would help us when working on this issue, please include it as a comment to this issue.