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, 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: phoneErrors.PHONE_REQUESTED, }) } } }) }