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.

Validate the lead form builder with the field schema

Rafi5a9f23d358ba
diff --git a/apps/web/ui/submitted-leads/form-builder/edit-lead-form-sheet.tsx b/apps/web/ui/submitted-leads/form-builder/edit-lead-form-sheet.tsx
index 6c42e4d482..54982c6cfa 100644
--- a/apps/web/ui/submitted-leads/form-builder/edit-lead-form-sheet.tsx
+++ b/apps/web/ui/submitted-leads/form-builder/edit-lead-form-sheet.tsx
@@ -24,11 +24,9 @@ import { Dispatch, SetStateAction, useMemo, useState } from "react";
import { toast } from "sonner";
import {
createLeadFormBuilderField,
- fromLeadFormBuilderField,
LeadFormBuilderField,
- LeadFormFieldType,
+ parseLeadFormBuilderFields,
toLeadFormBuilderField,
- validateLeadFormBuilderFields,
} from "./lead-form-builder-fields";
import { LeadFormFieldCard } from "./lead-form-field-card";
@@ -132,12 +130,12 @@ function EditLeadFormSheetForm({
const onSave = async () => {
if (!workspaceId) return;
- const error = validateLeadFormBuilderFields(fields);
+ const result = parseLeadFormBuilderFields(fields);
- if (error) {
- setErrorKey(error.key);
- setExpandedKey(error.key);
- toast.error(error.message);
+ if (!result.success) {
+ setErrorKey(result.key);
+ setExpandedKey(result.key);
+ toast.error(result.message);
return;
}
@@ -150,12 +148,7 @@ function EditLeadFormSheetForm({
(id) => !enabledGroupIds.includes(id),
),
referralFormData: {
- fields: fields.map((field, index) =>
- fromLeadFormBuilderField(
- field as LeadFormBuilderField & { type: LeadFormFieldType },
- index,
- ),
- ),
+ fields: result.fields,
},
});
};
diff --git a/apps/web/ui/submitted-leads/form-builder/lead-form-builder-fields.ts b/apps/web/ui/submitted-leads/form-builder/lead-form-builder-fields.ts
index f840c5881d..baef277593 100644
--- a/apps/web/ui/submitted-leads/form-builder/lead-form-builder-fields.ts
+++ b/apps/web/ui/submitted-leads/form-builder/lead-form-builder-fields.ts
@@ -168,65 +168,41 @@ export function fromLeadFormBuilderField(
}
}
-// Returns the first problem with the fields, or null when they are valid
-export function validateLeadFormBuilderFields(
+// Converts the fields and checks them with the form schema. On failure, it
+// returns the first field with a problem.
+export function parseLeadFormBuilderFields(
fields: LeadFormBuilderField[],
-): { key: string; message: string } | null {
- for (const field of fields) {
+):
+ | { success: true; fields: FormField[] }
+ | { success: false; key: string; message: string } {
+ const parsedFields: FormField[] = [];
+
+ for (const [position, field] of fields.entries()) {
if (!field.type) {
return {
+ success: false,
key: field.key,
message: "Select an input type for each field.",
};
}
- if (!field.label.trim()) {
- return { key: field.key, message: "Enter a label for each field." };
- }
-
- if (field.type === "select" || field.type === "multiSelect") {
- if (field.options.length < 2) {
- return {
- key: field.key,
- message: `"${field.label}" needs at least two options.`,
- };
- }
-
- if (field.options.some(({ label }) => !label.trim())) {
- return {
- key: field.key,
- message: `Enter a label for each option of "${field.label}".`,
- };
- }
- }
+ const result = formFieldSchema.safeParse(
+ fromLeadFormBuilderField({ ...field, type: field.type }, position),
+ );
- if (
- field.maxLength !== null &&
- (!Number.isInteger(field.maxLength) || field.maxLength < 1)
- ) {
- return {
- key: field.key,
- message: `The max characters of "${field.label}" must be a whole number of at least 1.`,
- };
- }
+ if (!result.success) {
+ const label = field.label.trim();
+ const { message } = result.error.issues[0];
- if (
- (field.min !== null && Number.isNaN(field.min)) ||
- (field.max !== null && Number.isNaN(field.max))
- ) {
return {
+ success: false,
key: field.key,
- message: `Enter a number for the limits of "${field.label}".`,
+ message: label ? `${label}: ${message}` : message,
};
}
- if (field.min !== null && field.max !== null && field.min > field.max) {
- return {
- key: field.key,
- message: `The minimum of "${field.label}" can't be more than its maximum.`,
- };
- }
+ parsedFields.push(result.data);
}
- return null;
+ return { success: true, fields: parsedFields };
}