refactor: A improvement fix related to optional phone validation (#22305)

* fix:A bug related to optional phone validation

* remove comment

* fix: updated phone validation

---------

Co-authored-by: Anik Dhabal Babu <81948346+anikdhabal@users.noreply.github.com>
Co-authored-by: Udit Takkar <53316345+Udit-takkar@users.noreply.github.com>
This commit is contained in:
Vansh Gilhotra
2025-07-24 23:03:49 +01:00
committed by GitHub
co-authored by Anik Dhabal Babu Udit Takkar
parent 6b9115dfde
commit 3228d8d869
@@ -276,8 +276,14 @@ function preprocess<T extends z.ZodType>({
}
if (bookingField.type === "phone") {
if (!bookingField.hidden && !(await phoneSchema.safeParseAsync(value)).success) {
ctx.addIssue({ code: z.ZodIssueCode.custom, message: m("invalid_number") });
// Determine if the phone field needs validation
const needsValidation = isRequired || (value && value.trim() !== "");
// Validate phone number if the field is not hidden and requires validation
if (!bookingField.hidden && needsValidation) {
if (!(await phoneSchema.safeParseAsync(value)).success) {
ctx.addIssue({ code: z.ZodIssueCode.custom, message: m("invalid_number") });
}
}
continue;
}