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
@@ -1,49 +1,41 @@
|
||||
import { defineMessage } from "react-intl"
|
||||
import { z } from "zod"
|
||||
|
||||
export {
|
||||
type AdditionalInfoFormSchema,
|
||||
additionalInfoFormSchema,
|
||||
findMyBookingErrors,
|
||||
type FindMyBookingFormSchema,
|
||||
findMyBookingFormSchema,
|
||||
}
|
||||
|
||||
defineMessage({
|
||||
defaultMessage: "Invalid booking number",
|
||||
})
|
||||
defineMessage({
|
||||
defaultMessage: "Booking number is required",
|
||||
})
|
||||
defineMessage({
|
||||
defaultMessage: "First name is required",
|
||||
})
|
||||
defineMessage({
|
||||
defaultMessage: "Last name is required",
|
||||
})
|
||||
defineMessage({
|
||||
defaultMessage: "Email address is required",
|
||||
})
|
||||
const findMyBookingErrors = {
|
||||
BOOKING_NUMBER_INVALID: "BOOKING_NUMBER_INVALID",
|
||||
BOOKING_NUMBER_REQUIRED: "BOOKING_NUMBER_REQUIRED",
|
||||
FIRST_NAME_REQUIRED: "FIRST_NAME_REQUIRED",
|
||||
LAST_NAME_REQUIRED: "LAST_NAME_REQUIRED",
|
||||
EMAIL_REQUIRED: "EMAIL_REQUIRED",
|
||||
} as const
|
||||
|
||||
const additionalInfoFormSchema = z.object({
|
||||
firstName: z.string().trim().max(250).min(1, {
|
||||
message: "First name is required",
|
||||
}),
|
||||
email: z.string().max(250).email({ message: "Email address is required" }),
|
||||
firstName: z
|
||||
.string()
|
||||
.trim()
|
||||
.max(250)
|
||||
.min(1, findMyBookingErrors.FIRST_NAME_REQUIRED),
|
||||
email: z.string().max(250).email(findMyBookingErrors.EMAIL_REQUIRED),
|
||||
})
|
||||
|
||||
const findMyBookingFormSchema = additionalInfoFormSchema.extend({
|
||||
confirmationNumber: z
|
||||
.string()
|
||||
.trim()
|
||||
.min(1, {
|
||||
message: "Booking number is required",
|
||||
})
|
||||
.regex(/^[0-9]+(-[0-9])?$/, {
|
||||
message: "Invalid booking number",
|
||||
}),
|
||||
lastName: z.string().trim().max(250).min(1, {
|
||||
message: "Last name is required",
|
||||
}),
|
||||
.min(1, findMyBookingErrors.BOOKING_NUMBER_REQUIRED)
|
||||
.regex(/^[0-9]+(-[0-9])?$/, findMyBookingErrors.BOOKING_NUMBER_INVALID),
|
||||
lastName: z
|
||||
.string()
|
||||
.trim()
|
||||
.max(250)
|
||||
.min(1, findMyBookingErrors.LAST_NAME_REQUIRED),
|
||||
})
|
||||
|
||||
type AdditionalInfoFormSchema = z.output<typeof additionalInfoFormSchema>
|
||||
|
||||
Reference in New Issue
Block a user