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.

Add clear error messages to the lead form field schemas

Rafi23d569bdbc84
diff --git a/apps/web/lib/zod/schemas/submitted-lead-form.ts b/apps/web/lib/zod/schemas/submitted-lead-form.ts
index ec8f769026..9b1133f79e 100644
--- a/apps/web/lib/zod/schemas/submitted-lead-form.ts
+++ b/apps/web/lib/zod/schemas/submitted-lead-form.ts
@@ -14,23 +14,36 @@ const fieldTypeSchema = z.enum([
export const fieldCommonSchema = z.object({
key: z.string().min(1),
- label: z.string().min(1),
+ label: z.string().min(1, "Enter a label for each field."),
required: z.boolean(),
locked: z.boolean(),
position: z.number().int().nonnegative(),
});
export const selectOptionSchema = z.object({
- label: z.string().min(1),
+ label: z.string().min(1, "Enter a label for each option."),
value: z.string().min(1),
});
+const selectOptionsSchema = z
+ .array(selectOptionSchema)
+ .min(2, "Add at least two options.");
+
+const maxLengthSchema = z
+ .number({ error: "Enter a number for the max characters." })
+ .int("The max characters must be a whole number.")
+ .min(1, "The max characters must be at least 1.");
+
+const numberLimitSchema = z.number({
+ error: "Enter a number for the minimum and maximum.",
+});
+
// Text
export const textFieldSchema = fieldCommonSchema.extend({
type: z.literal("text"),
constraints: z
.object({
- maxLength: z.number().int().min(1).optional(),
+ maxLength: maxLengthSchema.optional(),
pattern: z.string().optional(),
})
.optional(),
@@ -41,7 +54,7 @@ export const textareaFieldSchema = fieldCommonSchema.extend({
type: z.literal("textarea"),
constraints: z
.object({
- maxLength: z.number().int().min(1).optional(),
+ maxLength: maxLengthSchema.optional(),
})
.optional(),
});
@@ -49,7 +62,7 @@ export const textareaFieldSchema = fieldCommonSchema.extend({
// Select
export const selectFieldSchema = fieldCommonSchema.extend({
type: z.literal("select"),
- options: z.array(selectOptionSchema).min(2),
+ options: selectOptionsSchema,
});
// Country
@@ -65,7 +78,7 @@ export const dateFieldSchema = fieldCommonSchema.extend({
// Multiple Choices (Multi-select)
export const multiSelectFieldSchema = fieldCommonSchema.extend({
type: z.literal("multiSelect"),
- options: z.array(selectOptionSchema).min(2),
+ options: selectOptionsSchema,
});
// Number
@@ -73,8 +86,8 @@ export const numberFieldSchema = fieldCommonSchema.extend({
type: z.literal("number"),
constraints: z
.object({
- min: z.number().optional(),
- max: z.number().optional(),
+ min: numberLimitSchema.optional(),
+ max: numberLimitSchema.optional(),
})
.refine(
({ min, max }) => min === undefined || max === undefined || min <= max,