fix: extrapolate phone number parsing for re-usage in edit profile

move error messages to message handler
This commit is contained in:
Christian Andolf
2025-06-12 16:45:26 +02:00
parent e645b15c6e
commit 9da986f554
11 changed files with 62 additions and 45 deletions

View File

@@ -1,19 +1,24 @@
import { z } from "zod"
export const phoneErrors = {
PHONE_NUMBER_TOO_SHORT: "PHONE_NUMBER_TOO_SHORT",
PHONE_REQUESTED: "PHONE_REQUESTED",
} as const
export function phoneValidator(
msg = "Required field",
invalidMsg = "Invalid type"
) {
return z
.string({ invalid_type_error: invalidMsg, required_error: msg })
.min(5, { message: "The number you have entered is too short" })
.min(5, phoneErrors.PHONE_NUMBER_TOO_SHORT)
.superRefine((value, ctx) => {
if (value) {
const containsAlphabeticChars = /[a-z]/gi.test(value)
if (containsAlphabeticChars) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: "Please enter a valid phone number",
message: phoneErrors.PHONE_REQUESTED,
})
}
}