You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: documentation/docs/reference/rule.md
+64
Original file line number
Diff line number
Diff line change
@@ -25,3 +25,67 @@ keywords:
25
25
# Rule
26
26
27
27
A Rule or a Business Rule is a language element which captures some business rules of the language. They are used inside the domain layer to enforce the invariants.
28
+
It provides a structured way to encapsulate complex business conditions and exceptions, ensuring they are consistently enforced across the domain layer of your application.
29
+
30
+
## Syntax
31
+
32
+
Each Rule can throw a Domain Error when its associated condition is not satisfied. The Domain Error serves as an exception mechanism indicating a violation of business rules.
33
+
34
+
For instance, let's consider the following Domain Error:
Here, the `AccountCannotHaveNegativeBalanceRule` Rule encapsulates the invariant that an account's balance cannot become negative. If the amount to be subtracted from the balance would result in a negative value, the `isBrokenIf` function throws the `InsufficientBalanceError` Domain Error.
57
+
58
+
### Using isBrokenIf
59
+
60
+
The `isBrokenIf` function is central to a Domain Rule definition. This function accepts two arguments:
61
+
62
+
1. A boolean condition to be evaluated.
63
+
2. The arguments to be passed to the Domain Error, in case the condition is evaluated to `true`.
64
+
65
+
The second argument of `isBrokenIf` maps directly to the parameters of the Domain Error. In the `AccountCannotHaveNegativeBalanceRule` example above, `(balanceUpdated, amount)` are passed as the second argument, which are then utilized by the `InsufficientBalanceError`.
0 commit comments