From bbb86f2174507d744906829bdbacf00d1ff81ee5 Mon Sep 17 00:00:00 2001 From: Hariom Balhara Date: Fri, 17 May 2024 19:00:47 +0530 Subject: [PATCH] fix: Invalid phone error on prefill with plus(+) sign (#15046) * fix: Invalid phone error * Update packages/features/bookings/lib/getBookingResponsesSchema.test.ts --------- Co-authored-by: Udit Takkar <53316345+Udit-takkar@users.noreply.github.com> --- .../lib/getBookingResponsesSchema.test.ts | 56 ++++++++++++++++++- .../bookings/lib/getBookingResponsesSchema.ts | 4 ++ 2 files changed, 59 insertions(+), 1 deletion(-) diff --git a/packages/features/bookings/lib/getBookingResponsesSchema.test.ts b/packages/features/bookings/lib/getBookingResponsesSchema.test.ts index 5022fea425..7ccdc6857b 100644 --- a/packages/features/bookings/lib/getBookingResponsesSchema.test.ts +++ b/packages/features/bookings/lib/getBookingResponsesSchema.test.ts @@ -506,7 +506,7 @@ describe("getBookingResponsesSchema", () => { }) ); }); - test(`should succesfull give responses if phone type field value is valid`, async ({}) => { + test(`should successfully give responses if phone type field value is valid`, async ({}) => { const schema = getBookingResponsesSchema({ bookingFields: [ { @@ -543,6 +543,60 @@ describe("getBookingResponsesSchema", () => { }); }); + test(`should give parsed response if phone type field value starts with a space`, async ({}) => { + const schema = getBookingResponsesSchema({ + bookingFields: [ + { + name: "name", + type: "name", + required: true, + }, + { + name: "email", + type: "email", + required: true, + }, + { + name: "testPhone", + type: "phone", + required: true, + }, + ] as z.infer & z.BRAND<"HAS_SYSTEM_FIELDS">, + view: "ALL_VIEWS", + }); + const parsedResponses = await schema.safeParseAsync({ + email: "test@test.com", + name: "test", + // Space can come due to libraries considering + to be space + testPhone: " 919999999999", + }); + expect(parsedResponses.success).toBe(true); + if (!parsedResponses.success) { + throw new Error("Should not reach here"); + } + expect(parsedResponses.data).toEqual({ + email: "test@test.com", + name: "test", + testPhone: "+919999999999", + }); + + const parsedResponses2 = await schema.safeParseAsync({ + email: "test@test.com", + name: "test", + // Space can come due to libraries considering + to be space + testPhone: " 919999999999", + }); + expect(parsedResponses2.success).toBe(true); + if (!parsedResponses2.success) { + throw new Error("Should not reach here"); + } + expect(parsedResponses2.data).toEqual({ + email: "test@test.com", + name: "test", + testPhone: "+919999999999", + }); + }); + test("should fail parsing if phone field value is empty", async ({}) => { const schema = getBookingResponsesSchema({ bookingFields: [ diff --git a/packages/features/bookings/lib/getBookingResponsesSchema.ts b/packages/features/bookings/lib/getBookingResponsesSchema.ts index 4a6d9438b0..31b3c732f0 100644 --- a/packages/features/bookings/lib/getBookingResponsesSchema.ts +++ b/packages/features/bookings/lib/getBookingResponsesSchema.ts @@ -90,6 +90,10 @@ function preprocess({ parsedValue = JSON.parse(value); } catch (e) {} newResponses[field.name] = parsedValue; + } else if (field.type === "phone") { + // + in URL could be replaced with space, so we need to replace it back + // Replace the space(s) in the beginning with + as it is supposed to be provided in the beginning only + newResponses[field.name] = value.replace(/^ +/, "+"); } else { newResponses[field.name] = value; }