53 lines
1.5 KiB
TypeScript
53 lines
1.5 KiB
TypeScript
import { z } from "zod"
|
|
|
|
import { phoneValidator } from "@/utils/phoneValidator"
|
|
|
|
export const baseDetailsSchema = z.object({
|
|
countryCode: z.string(),
|
|
email: z.string().email(),
|
|
firstName: z.string(),
|
|
lastName: z.string(),
|
|
phoneNumber: phoneValidator(),
|
|
})
|
|
|
|
export const notJoinDetailsSchema = baseDetailsSchema.merge(
|
|
z.object({
|
|
join: z.literal(false),
|
|
zipCode: z.string().optional(),
|
|
dateOfBirth: z.string().optional(),
|
|
termsAccepted: z.boolean().default(false),
|
|
})
|
|
)
|
|
|
|
export const joinDetailsSchema = baseDetailsSchema.merge(
|
|
z.object({
|
|
join: z.literal(true),
|
|
zipCode: z.string().min(1, { message: "Zip code is required" }),
|
|
dateOfBirth: z.string().min(1, { message: "Date of birth is required" }),
|
|
termsAccepted: z.literal(true, {
|
|
errorMap: (err, ctx) => {
|
|
switch (err.code) {
|
|
case "invalid_literal":
|
|
return { message: "You must accept the terms and conditions" }
|
|
}
|
|
return { message: ctx.defaultError }
|
|
},
|
|
}),
|
|
})
|
|
)
|
|
|
|
export const guestDetailsSchema = z.discriminatedUnion("join", [
|
|
notJoinDetailsSchema,
|
|
joinDetailsSchema,
|
|
])
|
|
|
|
// For signed in users we accept partial or invalid data. Users cannot
|
|
// change their info in this flow, so we don't want to validate it.
|
|
export const signedInDetailsSchema = z.object({
|
|
countryCode: z.string().optional(),
|
|
email: z.string().optional(),
|
|
firstName: z.string().optional(),
|
|
lastName: z.string().optional(),
|
|
phoneNumber: z.string().optional(),
|
|
})
|