Rule Utilities
Author:
Kirill Gaiduk
Changed on:
10 July 2025
Overview
The `RuleUtils`
class in the `util-core`
bundle provides helper methods to streamline the process of retrieving and validating Rule properties. Using these utilities helps ensure that your Rules are configured correctly and reduces the risk of runtime errors caused by missing or invalid parameters.
Key points
- Flexible List Properties:
`rulePropAsList`
reliably parses a rule property into a`List`
, whether it's configured as a comma-separated string or a JSON array. This is ideal for properties that require multiple values. - Configuration Validation:
`validateRulePropsIsNotEmpty`
provides a simple, one-line way to ensure that all required properties have been provided to a rule, preventing runtime errors. - Fail-Fast Errors: When validation fails, the utility throws a
`PropertyNotFoundException`
with a clear error message. This stops the rule from executing with an invalid configuration and makes debugging easier.
Core Methods
`rulePropAsList`
To retrieve rule properties as a list, you can use the `RuleUtils.rulePropAsList`
method. This utility method is designed to handle several common formats for structuring list-based properties, providing flexibility when a "standard" format is not defined. Here’s what it supports:
- A JSON array literal of strings or objects (e.g.,
`["a", "b", "c"]`
). - A comma-separated string (e.g.,
`"a,b,c"`
).
Example:
Imagine a rule that needs a list of statuses to check. You could configure this property in the workflow in several ways:
- as a JSON array:
`"props": { "statusList": ["CREATED", "AWAITING_FULFILMENT"] }`
- as a comma-separated string:
`"props": { "statusList": "CREATED,AWAITING_FULFILMENT" }`
In your rule, you can reliably retrieve this property as a `List<String>`
using a single line of code:
1import com.fluentcommerce.util.core.RuleUtils;
2import java.util.List;
3
4//...
5
6@Override
7public void run(Context context) {
8 // Retrieves the "statusList" property, regardless of whether it's a
9 // JSON array or a comma-separated string.
10 List<String> statuses = RuleUtils.rulePropAsList(
11 context,
12 "statusList",
13 String.class
14 );
15
16 // Now you can work with the list
17 for (String status : statuses) {
18 context.action().log("Processing status: " + status);
19 }
20}
`validateRulePropsIsNotEmpty`
The `RuleUtils.validateRulePropsIsNotEmpty`
method is a convenient way to verify that one or more required properties have been provided to your rule. It checks that the properties exist in the context and that their values are not null or empty.
If a property is missing or empty, a `PropertyNotFoundException`
is thrown. This exception includes a descriptive error message indicating which property is missing, and it stops the rule execution immediately.
1import com.fluentcommerce.util.core.RuleUtils;
2
3//...
4
5@Override
6public void run(Context context) {
7 // Validate that all required properties are present before executing logic
8 RuleUtils.validateRulePropsIsNotEmpty(
9 context,
10 "endpointUrl",
11 "eventName",
12 "retryCount"
13 );
14
15 // If validation passes, you can safely retrieve the properties
16 String url = context.getProp("endpointUrl", String.class);
17 String eventName = context.getProp("eventName", String.class);
18
19 // ... rest of your rule logic
20}