Functions
openVALIDATION provides various functions. Let's say we want to check in a rule whether we have spent more money on shopping than our budget allows.
We have the following scheme:
YAML
JSON
1
shopping_list:
2
- article : text
3
price : 0
Copied!
1
{
2
shopping_list: [
3
{
4
article : "text",
5
price : 0
6
}
7
]
8
}
Copied!
And here's the rule that goes with it:
1
SUM OF shopping_list.price
2
AS expenses
3
4
the expenses MUST NOT EXCEED the budget of 20.
Copied!
First, we define a variable called Expenses. This variable uses the SUM OF function. The list of all prices from the shopping list is transferred to the function. The function SUM OF calculates a sum and thus has a return value of type Number. The Issues variable is automatically assigned this data type and can then be used within the rule.
The rule itself contains a numeric comparison operation. The whole thing looks like this:
1
var expenses = sum(shopping_list.map(a -> a.price))
2
3
If (expenses > 20 )
4
throw Error("the expenses MUST NOT EXCEED the budget of 20 €")
Copied!
Functions can of course also be used within a rule.
Copy link