80 lines
2.3 KiB
TypeScript
80 lines
2.3 KiB
TypeScript
import { z } from "zod"
|
|
|
|
import { phoneValidator } from "@/utils/phoneValidator"
|
|
|
|
const stringMatcher = /^[\p{L}\s\-\.]+$/u
|
|
|
|
const isValidString = (key: string) => stringMatcher.test(key)
|
|
|
|
export const baseDetailsSchema = z.object({
|
|
countryCode: z.string().min(1, { message: "Country is required" }),
|
|
email: z.string().email({ message: "Email address is required" }),
|
|
firstName: z
|
|
.string()
|
|
.min(1, { message: "First name is required" })
|
|
.refine(isValidString, {
|
|
message: "First name can't contain any special charachters",
|
|
}),
|
|
lastName: z
|
|
.string()
|
|
.min(1, { message: "Last name is required" })
|
|
.refine(isValidString, {
|
|
message: "Last name can't contain any special charachters",
|
|
}),
|
|
phoneNumber: phoneValidator(),
|
|
})
|
|
|
|
export const notJoinDetailsSchema = baseDetailsSchema.merge(
|
|
z.object({
|
|
join: z.literal<boolean>(false),
|
|
zipCode: z.string().optional(),
|
|
dateOfBirth: z.string().optional(),
|
|
membershipNo: z
|
|
.string()
|
|
.optional()
|
|
.refine((val) => {
|
|
if (val) {
|
|
return !val.match(/[^0-9]/g)
|
|
}
|
|
return true
|
|
}, "Only digits are allowed")
|
|
.refine((num) => {
|
|
if (num) {
|
|
return num.length === 14
|
|
}
|
|
return true
|
|
}, "Membership number needs to be 14 digits"),
|
|
})
|
|
)
|
|
|
|
export const joinDetailsSchema = baseDetailsSchema.merge(
|
|
z.object({
|
|
join: z.literal<boolean>(true),
|
|
zipCode: z.string().min(1, { message: "Zip code is required" }),
|
|
dateOfBirth: z.string().min(1, { message: "Date of birth is required" }),
|
|
membershipNo: z.string().default(""),
|
|
})
|
|
)
|
|
|
|
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().default(""),
|
|
email: z.string().default(""),
|
|
firstName: z.string().default(""),
|
|
lastName: z.string().default(""),
|
|
membershipNo: z.string().default(""),
|
|
phoneNumber: z.string().default(""),
|
|
join: z
|
|
.boolean()
|
|
.optional()
|
|
.transform((_) => false),
|
|
dateOfBirth: z.string().default(""),
|
|
zipCode: z.string().default(""),
|
|
})
|