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.

Extract modular validators for group move rules

Kiran Keac401e3eed0
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 e788b3b216..526426711e 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
@@ -119,7 +119,10 @@ function GroupAdditionalSettingsForm({
if (!group) return;
if (data.moveRules && data.moveRules.length > 0) {
try {
- validateGroupMoveRules(data.moveRules);
+ validateGroupMoveRules({
+ rules: data.moveRules,
+ destinationGroupId: group.id,
+ });
} catch (error) {
toast.error(error.message);
return;
diff --git a/apps/web/lib/api/groups/validate-group-move-rules.ts b/apps/web/lib/api/groups/validate-group-move-rules.ts
index c84ece07cb..1b255ea124 100644
--- a/apps/web/lib/api/groups/validate-group-move-rules.ts
+++ b/apps/web/lib/api/groups/validate-group-move-rules.ts
@@ -1,61 +1,74 @@
-import type { GroupMoveRules } from "@/lib/zod/schemas/group-move-workflows";
+import {
+ GROUP_MOVE_ATTRIBUTE_CONFIG,
+ GROUP_MOVE_ATTRIBUTE_VALIDATORS,
+ GROUP_MOVE_OPERATOR_VALIDATORS,
+ type GroupMoveCondition,
+ type GroupMoveRules,
+} from "@/lib/zod/schemas/group-move-workflows";
-export const validateGroupMoveRules = (rules?: GroupMoveRules) => {
+export const validateGroupMoveRules = ({
+ rules,
+ destinationGroupId,
+}: {
+ rules?: GroupMoveRules;
+ destinationGroupId: string;
+}) => {
if (!rules || rules.length === 0) {
return;
}
for (let i = 0; i < rules.length; i++) {
- const rule = rules[i];
-
- // Check if attribute is selected
- if (!rule.attribute) {
- throw new Error(`Rule ${i + 1}: Please select an activity.`);
- }
-
- // Check if value is set
- if (rule.value == null || rule.value === undefined) {
- throw new Error(`Rule ${i + 1}: Please enter a value.`);
- }
-
- // For gte operator, value should be a number greater than 0
- if (rule.operator === "gte") {
- if (
- typeof rule.value !== "number" ||
- isNaN(rule.value) ||
- rule.value <= 0
- ) {
- throw new Error(`Rule ${i + 1}: Please enter a value greater than 0.`);
- }
- }
-
- // For between operator, check min and max
- if (rule.operator === "between") {
- if (typeof rule.value !== "object" || rule.value === null) {
- throw new Error(`Rule ${i + 1}: Please enter a valid value.`);
- }
-
- const min = rule.value.min;
- const max = rule.value.max;
-
- if (min == null || min === undefined || isNaN(min) || min <= 0) {
- throw new Error(
- `Rule ${i + 1}: Please enter a minimum value greater than 0.`,
- );
- }
-
- if (max == null || max === undefined || isNaN(max) || max <= 0) {
- throw new Error(
- `Rule ${i + 1}: Please enter a maximum value (limit) greater than 0.`,
- );
- }
-
- // Ensure max is greater than min
- if (max <= min) {
- throw new Error(
- `Rule ${i + 1}: Maximum value must be greater than minimum value.`,
- );
- }
- }
+ validateRule({
+ rule: rules[i],
+ ruleIndex: i,
+ destinationGroupId,
+ });
}
};
+
+// Validates a single group move rule
+const validateRule = ({
+ rule,
+ ruleIndex,
+ destinationGroupId,
+}: {
+ rule: GroupMoveCondition;
+ ruleIndex: number;
+ destinationGroupId: string;
+}) => {
+ if (!rule.attribute) {
+ throw new Error(`Rule ${ruleIndex + 1}: Please select an activity.`);
+ }
+
+ // Check if operator is valid for the attribute
+ const allowedOperators = GROUP_MOVE_ATTRIBUTE_CONFIG[rule.attribute]
+ .operators as readonly string[];
+
+ if (!allowedOperators.includes(rule.operator)) {
+ throw new Error(
+ `Rule ${ruleIndex + 1}: Operator "${rule.operator}" is not valid for this condition.`,
+ );
+ }
+
+ // Check if value is set
+ if (rule.value == null || rule.value === undefined) {
+ throw new Error(`Rule ${ruleIndex + 1}: Please enter a value.`);
+ }
+
+ // Validate value shape/constraints for the selected operator
+ const validateOperator =
+ GROUP_MOVE_OPERATOR_VALIDATORS[
+ rule.operator as keyof typeof GROUP_MOVE_OPERATOR_VALIDATORS
+ ];
+
+ if (validateOperator) {
+ validateOperator(rule, ruleIndex);
+ }
+
+ // Validate attribute-specific constraints (business rules)
+ GROUP_MOVE_ATTRIBUTE_VALIDATORS[rule.attribute]({
+ rule,
+ ruleIndex,
+ destinationGroupId,
+ });
+};
diff --git a/apps/web/lib/types.ts b/apps/web/lib/types.ts
index 3e34281ea5..0778ebd271 100644
--- a/apps/web/lib/types.ts
+++ b/apps/web/lib/types.ts
@@ -739,11 +739,6 @@ export type WorkflowComparisonOperator =
export type WorkflowAction = z.infer<typeof workflowActionSchema>;
-export type OperatorFn = (
- aV: number,
- cV: number | { min: number; max?: number },
-) => boolean;
-
export type BountySubmissionsQueryFilters = z.infer<
typeof getBountySubmissionsQuerySchema
>;
diff --git a/apps/web/lib/zod/schemas/group-move-workflows.ts b/apps/web/lib/zod/schemas/group-move-workflows.ts
index 12ec7c097b..2068b2acb9 100644
--- a/apps/web/lib/zod/schemas/group-move-workflows.ts
+++ b/apps/web/lib/zod/schemas/group-move-workflows.ts
@@ -54,6 +54,61 @@ export const groupMoveConditionSchema = z.object({
export const groupMoveRulesSchema = z.array(groupMoveConditionSchema);
+export const GROUP_MOVE_OPERATOR_VALIDATORS = {
+ gte(rule: GroupMoveCondition, ruleIndex: number) {
+ if (
+ typeof rule.value !== "number" ||
+ isNaN(rule.value) ||
+ rule.value <= 0
+ ) {
+ throw new Error(
+ `Rule ${ruleIndex + 1}: Please enter a value greater than 0.`,
+ );
+ }
+ },
+
+ between(rule: GroupMoveCondition, ruleIndex: number) {
+ if (typeof rule.value !== "object" || rule.value === null) {
+ throw new Error(`Rule ${ruleIndex + 1}: Please enter a valid value.`);
+ }
+
+ const min = rule.value.min;
+ const max = rule.value.max;
+
+ if (min == null || min === undefined || isNaN(min) || min <= 0) {
+ throw new Error(
+ `Rule ${ruleIndex + 1}: Please enter a minimum value greater than 0.`,
+ );
+ }
+
+ if (max == null || max === undefined || isNaN(max) || max <= 0) {
+ throw new Error(
+ `Rule ${ruleIndex + 1}: Please enter a maximum value (limit) greater than 0.`,
+ );
+ }
+
+ // Ensure max is greater than min
+ if (max <= min) {
+ throw new Error(
+ `Rule ${ruleIndex + 1}: Maximum value must be greater than minimum value.`,
+ );
+ }
+ },
+} as const;
+
+// Add business rules for each attribute
+// Keeping this empty for now
+export const GROUP_MOVE_ATTRIBUTE_VALIDATORS: Record<
+ GroupMoveAttribute,
+ GroupMoveAttributeValidator
+> = {
+ // Operator validators already cover performance value shape/constraints.
+ totalLeads: () => {},
+ totalConversions: () => {},
+ totalSaleAmount: () => {},
+ totalCommissions: () => {},
+};
+
export type GroupMoveCondition = z.infer<typeof groupMoveConditionSchema>;
export type GroupMoveRules = z.infer<typeof groupMoveRulesSchema>;
@@ -67,3 +122,13 @@ export type GroupMoveAttributeConfig = {
inputType: "number" | "currency";
operators: readonly GroupMoveOperator[];
};
+
+type GroupMoveAttributeValidatorArgs = {
+ rule: GroupMoveCondition;
+ ruleIndex: number;
+ destinationGroupId: string;
+};
+
+type GroupMoveAttributeValidator = (
+ params: GroupMoveAttributeValidatorArgs,
+) => void;
diff --git a/apps/web/lib/zod/schemas/workflows.ts b/apps/web/lib/zod/schemas/workflows.ts
index f9a5ad896c..749ee39ed5 100644
--- a/apps/web/lib/zod/schemas/workflows.ts
+++ b/apps/web/lib/zod/schemas/workflows.ts
@@ -1,11 +1,15 @@
import {
- OperatorFn,
WorkflowComparisonOperator,
WorkflowConditionAttribute,
} from "@/lib/types";
import { WorkflowTrigger } from "@prisma/client";
import * as z from "zod/v4";
+type OperatorFn = (
+ aV: number,
+ cV: number | { min: number; max?: number },
+) => boolean;
+
export const WORKFLOW_ATTRIBUTES = [
"totalLeads",
"totalConversions",