Allow processing of additional object types by regex function in Rules
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 6.5k
- Forks
- 787
- PR merge metrics
- No merged PRs in 30d
Description
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 ===
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with st2common/operators.py's regex handling and the payload processing in util/payload.py; reproduce the list-valued criterion with st2-rule-tester. Done means the regex criterion can process the demonstrated list and match both terms without regressing existing string and bytes behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- backend, devops
- Issue type
- Feature
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100