zerot
    Preparing search index...

    Function businessRule

    • Creates a business rule condition. This condition allows defining custom business logic that must pass for a method to execute or complete.

      Parameters

      • description: string

        A descriptive message for the business rule, used in case of violation.

      • rule: (input: any, context: AuthContext) => boolean | Promise<boolean>

        A function that implements the business logic. It takes input and authentication context, and returns a boolean or a Promise resolving to a boolean.

      Returns (input: any, context: AuthContext) => Promise<boolean>

      A condition function that takes input and authentication context, and returns a Promise resolving to a boolean.

      If the business rule is violated.

      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}`);
      }
      }