A descriptive message for the business rule, used in case of violation.
A function that implements the business logic. It takes input and authentication context, and returns a boolean or a Promise resolving to a boolean.
A condition function that takes input and authentication context, and returns a Promise resolving to a boolean.
class OrderService {
@contract({
requires: [
businessRule(
"Order value must be positive",
(input: { value: number }) => input.value > 0
),
businessRule(
"User must have sufficient balance",
async (input: { userId: string; value: number }, context: AuthContext) => {
const userBalance = await getUserBalance(input.userId);
return userBalance >= input.value;
}
),
],
})
async placeOrder(order: { userId: string; value: number }, context: AuthContext) {
// Logic to place the order
console.log(`Order placed for user ${order.userId} with value ${order.value}`);
}
}
Creates a business rule condition. This condition allows defining custom business logic that must pass for a method to execute or complete.