fix(SW-2385): handle error messages provided from zod validation in forms client side

This commit is contained in:
Christian Andolf
2025-04-15 15:04:32 +02:00
committed by Michael Zetterberg
parent 2648b17744
commit 595eb575d7
7 changed files with 257 additions and 99 deletions
@@ -3,35 +3,45 @@ import { z } from "zod"
import { passwordValidator } from "@/utils/zod/passwordValidator"
import { phoneValidator } from "@/utils/zod/phoneValidator"
const countryRequiredMsg = "Country is required"
export const signupErrors = {
COUNTRY_REQUIRED: "COUNTRY_REQUIRED",
FIRST_NAME_REQUIRED: "FIRST_NAME_REQUIRED",
LAST_NAME_REQUIRED: "LAST_NAME_REQUIRED",
PHONE_REQUIRED: "PHONE_REQUIRED",
PHONE_REQUESTED: "PHONE_REQUESTED",
BIRTH_DATE_REQUIRED: "BIRTH_DATE_REQUIRED",
PASSWORD_REQUIRED: "PASSWORD_REQUIRED",
TERMS_REQUIRED: "TERMS_REQUIRED",
} as const
export const signUpSchema = z.object({
firstName: z.string().max(250).trim().min(1, {
message: "First name is required",
}),
lastName: z.string().max(250).trim().min(1, {
message: "Last name is required",
}),
firstName: z
.string()
.max(250)
.trim()
.min(1, signupErrors.FIRST_NAME_REQUIRED),
lastName: z.string().max(250).trim().min(1, signupErrors.LAST_NAME_REQUIRED),
email: z.string().max(250).email(),
phoneNumber: phoneValidator(
"Phone is required",
"Please enter a valid phone number"
signupErrors.PHONE_REQUIRED,
signupErrors.PHONE_REQUESTED
),
dateOfBirth: z.string().min(1, {
message: "Date of birth is required",
message: signupErrors.BIRTH_DATE_REQUIRED,
}),
address: z.object({
countryCode: z
.string({
required_error: countryRequiredMsg,
invalid_type_error: countryRequiredMsg,
required_error: signupErrors.COUNTRY_REQUIRED,
invalid_type_error: signupErrors.COUNTRY_REQUIRED,
})
.min(1, countryRequiredMsg),
.min(1, signupErrors.COUNTRY_REQUIRED),
zipCode: z.string().min(1),
}),
password: passwordValidator("Password is required"),
termsAccepted: z.boolean().refine((value) => value === true, {
message: "You must accept the terms and conditions",
}),
password: passwordValidator(signupErrors.PASSWORD_REQUIRED),
termsAccepted: z
.boolean()
.refine((value) => value === true, signupErrors.TERMS_REQUIRED),
})
export type SignUpSchema = z.infer<typeof signUpSchema>