fix(SW-2385): handle error messages provided from zod validation in forms client side
This commit is contained in:
committed by
Michael Zetterberg
parent
2648b17744
commit
595eb575d7
@@ -11,22 +11,37 @@ const stringMatcher =
|
||||
|
||||
const isValidString = (key: string) => stringMatcher.test(key)
|
||||
|
||||
export const roomOneErrors = {
|
||||
COUNTRY_REQUIRED: "COUNTRY_REQUIRED",
|
||||
FIRST_NAME_REQUIRED: "FIRST_NAME_REQUIRED",
|
||||
FIRST_NAME_SPECIAL_CHARACTERS: "FIRST_NAME_SPECIAL_CHARACTERS",
|
||||
LAST_NAME_REQUIRED: "LAST_NAME_REQUIRED",
|
||||
LAST_NAME_SPECIAL_CHARACTERS: "LAST_NAME_SPECIAL_CHARACTERS",
|
||||
PHONE_REQUIRED: "PHONE_REQUIRED",
|
||||
PHONE_REQUESTED: "PHONE_REQUESTED",
|
||||
EMAIL_REQUIRED: "EMAIL_REQUIRED",
|
||||
MEMBERSHIP_NO_ONLY_DIGITS: "MEMBERSHIP_NO_ONLY_DIGITS",
|
||||
MEMBERSHIP_NO_INVALID: "MEMBERSHIP_NO_INVALID",
|
||||
ZIP_CODE_REQUIRED: "ZIP_CODE_REQUIRED",
|
||||
BIRTH_DATE_REQUIRED: "BIRTH_DATE_REQUIRED",
|
||||
BIRTH_DATE_AGE_18: "BIRTH_DATE_AGE_18",
|
||||
} as const
|
||||
|
||||
export const baseDetailsSchema = z.object({
|
||||
countryCode: z.string().min(1, { message: "Country is required" }),
|
||||
email: z.string().email({ message: "Email address is required" }),
|
||||
countryCode: z.string().min(1, roomOneErrors.COUNTRY_REQUIRED),
|
||||
email: z.string().email(roomOneErrors.EMAIL_REQUIRED),
|
||||
firstName: z
|
||||
.string()
|
||||
.min(1, { message: "First name is required" })
|
||||
.refine(isValidString, {
|
||||
message: "First name can't contain any special characters",
|
||||
}),
|
||||
.min(1, roomOneErrors.FIRST_NAME_REQUIRED)
|
||||
.refine(isValidString, roomOneErrors.FIRST_NAME_SPECIAL_CHARACTERS),
|
||||
lastName: z
|
||||
.string()
|
||||
.min(1, { message: "Last name is required" })
|
||||
.refine(isValidString, {
|
||||
message: "Last name can't contain any special characters",
|
||||
}),
|
||||
phoneNumber: phoneValidator(),
|
||||
.min(1, roomOneErrors.LAST_NAME_REQUIRED)
|
||||
.refine(isValidString, roomOneErrors.LAST_NAME_SPECIAL_CHARACTERS),
|
||||
phoneNumber: phoneValidator(
|
||||
roomOneErrors.PHONE_REQUIRED,
|
||||
roomOneErrors.PHONE_REQUESTED
|
||||
),
|
||||
specialRequest: specialRequestSchema,
|
||||
})
|
||||
|
||||
@@ -43,32 +58,29 @@ export const notJoinDetailsSchema = baseDetailsSchema.merge(
|
||||
return !val.match(/[^0-9]/g)
|
||||
}
|
||||
return true
|
||||
}, "Only digits are allowed")
|
||||
}, roomOneErrors.MEMBERSHIP_NO_ONLY_DIGITS)
|
||||
.refine((num) => {
|
||||
if (num) {
|
||||
return num.match(/^30812(?!(0|1|2))[0-9]{9}$/)
|
||||
}
|
||||
return true
|
||||
}, "Invalid membership number format"),
|
||||
}, roomOneErrors.MEMBERSHIP_NO_INVALID),
|
||||
})
|
||||
)
|
||||
|
||||
export const joinDetailsSchema = baseDetailsSchema.merge(
|
||||
z.object({
|
||||
join: z.literal<boolean>(true),
|
||||
zipCode: z.string().min(1, { message: "Zip code is required" }),
|
||||
zipCode: z.string().min(1, roomOneErrors.ZIP_CODE_REQUIRED),
|
||||
dateOfBirth: z
|
||||
.string()
|
||||
.min(1, { message: "Date of birth is required" })
|
||||
.refine(
|
||||
(date) => {
|
||||
const today = dt()
|
||||
const dob = dt(date)
|
||||
const age = today.diff(dob, "year")
|
||||
return age >= 18
|
||||
},
|
||||
{ message: "Must be at least 18 years of age to continue" }
|
||||
),
|
||||
.min(1, roomOneErrors.BIRTH_DATE_REQUIRED)
|
||||
.refine((date) => {
|
||||
const today = dt()
|
||||
const dob = dt(date)
|
||||
const age = today.diff(dob, "year")
|
||||
return age >= 18
|
||||
}, roomOneErrors.BIRTH_DATE_AGE_18),
|
||||
membershipNo: z.string().default(""),
|
||||
})
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user