(
constraints: {
isEmailUnique?: (email: string) => Promise<boolean>;
} = {}
)
| 26 | import { rootPath } from "~/utils/pathBuilder"; |
| 27 | |
| 28 | function createSchema( |
| 29 | constraints: { |
| 30 | isEmailUnique?: (email: string) => Promise<boolean>; |
| 31 | } = {} |
| 32 | ) { |
| 33 | return z |
| 34 | .object({ |
| 35 | name: z.string().min(3, "Your name must be at least 3 characters").max(50), |
| 36 | email: z |
| 37 | .string() |
| 38 | .email() |
| 39 | .superRefine((email, ctx) => { |
| 40 | if (constraints.isEmailUnique === undefined) { |
| 41 | //client-side validation skips this |
| 42 | ctx.addIssue({ |
| 43 | code: z.ZodIssueCode.custom, |
| 44 | message: conform.VALIDATION_UNDEFINED, |
| 45 | }); |
| 46 | } else { |
| 47 | // Tell zod this is an async validation by returning the promise |
| 48 | return constraints.isEmailUnique(email).then((isUnique) => { |
| 49 | if (isUnique) { |
| 50 | return; |
| 51 | } |
| 52 | |
| 53 | ctx.addIssue({ |
| 54 | code: z.ZodIssueCode.custom, |
| 55 | message: "Email is already being used by a different account", |
| 56 | }); |
| 57 | }); |
| 58 | } |
| 59 | }), |
| 60 | confirmEmail: z.string(), |
| 61 | referralSource: z.string().optional(), |
| 62 | }) |
| 63 | .refine((value) => value.email === value.confirmEmail, { |
| 64 | message: "Emails must match", |
| 65 | path: ["confirmEmail"], |
| 66 | }); |
| 67 | } |
| 68 | |
| 69 | export const action: ActionFunction = async ({ request }) => { |
| 70 | const userId = await requireUserId(request); |
no test coverage detected
searching dependent graphs…