Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

Allow gte conditions of 0 and throw DubApiError from workflow validation.

Kiran Kc8588ca0fd74
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/groups/[groupSlug]/settings/group-additional-settings.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/groups/[groupSlug]/settings/group-additional-settings.tsx
index dfa1440c34..293cbb8ac3 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/groups/[groupSlug]/settings/group-additional-settings.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/groups/[groupSlug]/settings/group-additional-settings.tsx
@@ -1,7 +1,6 @@
"use client";
import { findGroupsWithMatchingRules } from "@/lib/api/groups/find-groups-with-matching-rules";
-import { validateWorkflowConditions } from "@/lib/api/workflows/validate-workflow-conditions";
import { PAYOUT_HOLDING_PERIOD_DAYS } from "@/lib/constants/payouts";
import { mutatePrefix } from "@/lib/swr/mutate";
import { useApiMutation } from "@/lib/swr/use-api-mutation";
@@ -118,17 +117,8 @@ function GroupAdditionalSettingsForm({
const onSubmit = async (data: FormData) => {
if (!group) return;
if (data.moveRules && data.moveRules.length > 0) {
- try {
- await validateWorkflowConditions({
- conditions: data.moveRules,
- workflowType: "moveGroup",
- });
- } catch (error) {
- toast.error(
- error instanceof Error ? error.message : "Invalid group move rules.",
- );
- return;
- }
+ // TODO
+ // Add a client-only validation for the move rules
if (groups) {
const groupsWithMatchingRules = findGroupsWithMatchingRules({
diff --git a/apps/web/lib/api/workflows/operator-definitions.ts b/apps/web/lib/api/workflows/operator-definitions.ts
index 55cdf3a75d..e06d565a55 100644
--- a/apps/web/lib/api/workflows/operator-definitions.ts
+++ b/apps/web/lib/api/workflows/operator-definitions.ts
@@ -13,8 +13,8 @@ export const WORKFLOW_OPERATORS = {
name: "gte",
label: "at least",
validate(value: ConditionValue) {
- if (typeof value !== "number" || isNaN(value) || value <= 0) {
- throw new Error("Please enter a value greater than 0.");
+ if (typeof value !== "number" || isNaN(value) || value < 0) {
+ throw new Error("Please enter a value greater than or equal to 0.");
}
},
evaluate(attributeValue: number, conditionValue: ConditionValue) {
diff --git a/apps/web/lib/api/workflows/validate-workflow-conditions.ts b/apps/web/lib/api/workflows/validate-workflow-conditions.ts
index bce6a99dd2..2ed705f0b5 100644
--- a/apps/web/lib/api/workflows/validate-workflow-conditions.ts
+++ b/apps/web/lib/api/workflows/validate-workflow-conditions.ts
@@ -3,6 +3,7 @@ import { GROUP_MOVE_ATTRIBUTES } from "@/lib/api/workflows/move-group/schema";
import { WORKFLOW_OPERATORS } from "@/lib/api/workflows/operator-definitions";
import { SEND_CAMPAIGN_ATTRIBUTES } from "@/lib/api/workflows/send-campaign/schema";
import type { WorkflowType } from "@/lib/api/workflows/types";
+import { DubApiError } from "../errors";
// Map of workflow type to its attributes
const WORKFLOW_TYPE_ATTRIBUTES = {
@@ -38,9 +39,10 @@ export async function validateWorkflowConditions({
const conditionIndex = i;
if (!condition.attribute) {
- throw new Error(
- `Condition ${conditionIndex + 1}: Please select an activity.`,
- );
+ throw new DubApiError({
+ code: "bad_request",
+ message: `Condition ${conditionIndex + 1}: Please select an activity.`,
+ });
}
// Find the attribute definition
@@ -48,7 +50,10 @@ export async function validateWorkflowConditions({
attributes[condition.attribute as keyof typeof attributes];
if (!attributeDefinition) {
- throw new Error(`Condition ${conditionIndex + 1}: Invalid activity.`);
+ throw new DubApiError({
+ code: "bad_request",
+ message: `Condition ${conditionIndex + 1}: Invalid activity.`,
+ });
}
// Find the operator definition
@@ -56,7 +61,10 @@ export async function validateWorkflowConditions({
WORKFLOW_OPERATORS[condition.operator as keyof typeof WORKFLOW_OPERATORS];
if (!operatorDefinition) {
- throw new Error(`Condition ${conditionIndex + 1}: Invalid operator.`);
+ throw new DubApiError({
+ code: "bad_request",
+ message: `Condition ${conditionIndex + 1}: Invalid operator.`,
+ });
}
if (
@@ -66,21 +74,26 @@ export async function validateWorkflowConditions({
) {
const operatorLabel = operatorDefinition?.label ?? condition.operator;
- throw new Error(
- `Operator "${operatorLabel}" is not valid for the activity "${condition.attribute}".`,
- );
+ throw new DubApiError({
+ code: "bad_request",
+ message: `Operator "${operatorLabel}" is not valid for the activity "${condition.attribute}".`,
+ });
}
if (condition.value == null || condition.value === undefined) {
- throw new Error(`Condition ${conditionIndex + 1}: Please enter a value.`);
+ throw new DubApiError({
+ code: "bad_request",
+ message: `Condition ${conditionIndex + 1}: Please enter a value.`,
+ });
}
try {
operatorDefinition.validate(condition.value as any);
} catch (error) {
- throw new Error(
- `Condition ${conditionIndex + 1}: ${error instanceof Error ? error.message : "Invalid value."}`,
- );
+ throw new DubApiError({
+ code: "bad_request",
+ message: `Condition ${conditionIndex + 1}: ${error instanceof Error ? error.message : "Invalid value."}`,
+ });
}
}
}
diff --git a/apps/web/tests/workflows/comparison-operators.test.ts b/apps/web/tests/workflows/comparison-operators.test.ts
index 89ed2abd0c..4ca46278bc 100644
--- a/apps/web/tests/workflows/comparison-operators.test.ts
+++ b/apps/web/tests/workflows/comparison-operators.test.ts
@@ -44,18 +44,22 @@ describe("WORKFLOW_OPERATORS.between.evaluate", () => {
describe("WORKFLOW_OPERATORS.gte.validate", () => {
const { validate } = WORKFLOW_OPERATORS.gte;
- it("accepts a positive number", () => {
+ it("accepts a non-negative number", () => {
+ expect(() => validate(0)).not.toThrow();
expect(() => validate(1)).not.toThrow();
expect(() => validate(100)).not.toThrow();
});
- it("rejects non-number, NaN, and non-positive values", () => {
+ it("rejects non-number, NaN, and negative values", () => {
expect(() => validate({ min: 1, max: 5 })).toThrow(
- "Please enter a value greater than 0.",
+ "Please enter a value greater than or equal to 0.",
+ );
+ expect(() => validate(NaN)).toThrow(
+ "Please enter a value greater than or equal to 0.",
+ );
+ expect(() => validate(-1)).toThrow(
+ "Please enter a value greater than or equal to 0.",
);
- expect(() => validate(NaN)).toThrow("Please enter a value greater than 0.");
- expect(() => validate(0)).toThrow("Please enter a value greater than 0.");
- expect(() => validate(-1)).toThrow("Please enter a value greater than 0.");
});
});