Skip to content

Commit a1d44f0

Browse files
committed
refactor(webapp): sequence account email validation
1 parent f0e533e commit a1d44f0

2 files changed

Lines changed: 38 additions & 42 deletions

File tree

apps/webapp/app/routes/account._index/route.tsx

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -43,31 +43,29 @@ function createSchema(
4343
.string({ required_error: "You must enter a name" })
4444
.min(2, "Your name must be at least 2 characters long")
4545
.max(50),
46-
email: emailSchema.superRefine((email, ctx) => {
47-
if (email.length > MAX_EMAIL_LENGTH) {
48-
return;
49-
}
50-
51-
if (constraints.isEmailUnique === undefined) {
52-
//client-side validation skips this
53-
ctx.addIssue({
54-
code: z.ZodIssueCode.custom,
55-
message: conformZodMessage.VALIDATION_UNDEFINED,
56-
});
57-
} else {
58-
// Tell zod this is an async validation by returning the promise
59-
return constraints.isEmailUnique(email).then((isUnique) => {
60-
if (isUnique) {
61-
return;
62-
}
63-
46+
email: emailSchema.pipe(
47+
z.string().superRefine((email, ctx) => {
48+
if (constraints.isEmailUnique === undefined) {
49+
//client-side validation skips this
6450
ctx.addIssue({
6551
code: z.ZodIssueCode.custom,
66-
message: "Email is already being used by a different account",
52+
message: conformZodMessage.VALIDATION_UNDEFINED,
6753
});
68-
});
69-
}
70-
}),
54+
} else {
55+
// Tell zod this is an async validation by returning the promise
56+
return constraints.isEmailUnique(email).then((isUnique) => {
57+
if (isUnique) {
58+
return;
59+
}
60+
61+
ctx.addIssue({
62+
code: z.ZodIssueCode.custom,
63+
message: "Email is already being used by a different account",
64+
});
65+
});
66+
}
67+
})
68+
),
7169
marketingEmails: z.preprocess((value) => value === "on", z.boolean()),
7270
});
7371
}

apps/webapp/app/routes/confirm-basic-details.tsx

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -73,29 +73,27 @@ function createSchema(
7373
return z
7474
.object({
7575
name: z.string().min(3, "Your name must be at least 3 characters").max(50),
76-
email: emailSchema.superRefine((email, ctx) => {
77-
if (email.length > MAX_EMAIL_LENGTH) {
78-
return;
79-
}
80-
81-
if (constraints.isEmailUnique === undefined) {
82-
ctx.addIssue({
83-
code: z.ZodIssueCode.custom,
84-
message: conformZodMessage.VALIDATION_UNDEFINED,
85-
});
86-
} else {
87-
return constraints.isEmailUnique(email).then((isUnique) => {
88-
if (isUnique) {
89-
return;
90-
}
91-
76+
email: emailSchema.pipe(
77+
z.string().superRefine((email, ctx) => {
78+
if (constraints.isEmailUnique === undefined) {
9279
ctx.addIssue({
9380
code: z.ZodIssueCode.custom,
94-
message: "Email is already being used by a different account",
81+
message: conformZodMessage.VALIDATION_UNDEFINED,
82+
});
83+
} else {
84+
return constraints.isEmailUnique(email).then((isUnique) => {
85+
if (isUnique) {
86+
return;
87+
}
88+
89+
ctx.addIssue({
90+
code: z.ZodIssueCode.custom,
91+
message: "Email is already being used by a different account",
92+
});
9593
});
96-
});
97-
}
98-
}),
94+
}
95+
})
96+
),
9997
confirmEmail: emailSchema,
10098
referralSource: z.string().optional(),
10199
referralSourceOther: z.string().optional(),

0 commit comments

Comments
 (0)