Transaction Rule Decisions
HardParsing ยท Stripe
Given a list of rules and a list of transactions (dicts), return one decision per transaction. A rule is ACTION condition, where ACTION is BLOCK or REVIEW. A transaction receives the action of the first rule whose condition it satisfies, or "ALLOW" if none does.
A condition compares a field to a literal with ==, !=, <, <=, >, >=. Comparisons combine with AND, OR and parentheses, and AND binds tighter than OR. A comparison on a field the transaction lacks is false, even with !=. Number literals are integers; string literals are single-quoted and may contain spaces, keywords or symbols.
Example 1
- Input:
- rules = ["BLOCK country == 'RU' OR amount > 900 AND card == 'new'", "REVIEW (country == 'RU' OR amount > 900) AND memo == 'gift'"], transactions = [{"amount": 950, "card": "new", "country": "US"}, {"amount": 950, "card": "old", "country": "US"}, {"amount": 20, "card": "old", "country": "RU"}, {"amount": 950, "card": "old", "country": "US", "memo": "gift"}]
- Output:
- ["BLOCK", "ALLOW", "BLOCK", "REVIEW"]
- Explanation
- The first rule reads as
country == 'RU' OR (amount > 900 AND card == 'new'), so only the first and third transactions match it.
Example 2
- Input:
- rules = ["REVIEW memo == 'rent AND fees'"], transactions = [{"memo": "rent AND fees"}, {"memo": "rent"}]
- Output:
- ["REVIEW", "ALLOW"]
Example 3
- Input:
- rules = ["BLOCK risk != 'low'"], transactions = [{"amount": 5}, {"risk": "high"}, {"risk": "low"}]
- Output:
- ["ALLOW", "BLOCK", "ALLOW"]
- Explanation
- The first transaction has no
riskfield, so the comparison is false and no rule matches.
Constraints
0 <= rules.length <= 2000 <= transactions.length <= 1000- Every rule is well formed; field names are lowercase or mixed-case words made of letters, digits and
_ <,<=,>,>=are used only with integer literals, and a field compared with an integer holds an integer- String literals contain no
'; integer literals may be negative - Whitespace between tokens is arbitrary and may be absent around operators and parentheses