Scheduling infrastructure for absolutely everyone.
fix: Prefill of Phone type option field (#16086)
Hariom Balhara5ce96c77045c
diff --git a/packages/features/bookings/lib/getBookingResponsesSchema.test.ts b/packages/features/bookings/lib/getBookingResponsesSchema.test.tsindex c40712030c8..8e0529773a6 100644--- a/packages/features/bookings/lib/getBookingResponsesSchema.test.ts+++ b/packages/features/bookings/lib/getBookingResponsesSchema.test.ts@@ -990,11 +990,108 @@ describe("getBookingResponsesSchema", () => {test.todo("select");test.todo("textarea");test.todo("number");- test.todo("radioInput");test.todo("checkbox");test.todo("radio");test.todo("boolean");});++describe("validate radioInput type field", () => {+ test(`should fail parsing if invalid phone number is provided`, async ({}) => {+ const schema = getBookingResponsesSchema({+ bookingFields: [+ {+ name: "name",+ type: "name",+ required: true,+ },+ {+ name: "email",+ type: "email",+ required: true,+ },+ {+ name: "radioInput",+ type: "radioInput",+ required: true,+ optionsInputs: {+ phone: {+ type: "phone",+ },+ },+ },+ ] as z.infer<typeof eventTypeBookingFields> & z.BRAND<"HAS_SYSTEM_FIELDS">,+ view: "ALL_VIEWS",+ });+ const parsedResponses = await schema.safeParseAsync({+ email: "[email protected]",+ name: "test",+ radioInput: JSON.stringify({+ value: "phone",+ optionValue: " 9999999999",+ }),+ });+ expect(parsedResponses.success).toBe(false);+ if (parsedResponses.success) {+ throw new Error("Should not reach here");+ }+ expect(parsedResponses.error.issues[0]).toEqual(+ expect.objectContaining({+ code: "custom",+ message: `{radioInput}${CUSTOM_PHONE_VALIDATION_ERROR_MSG}`,+ })+ );+ });++ test(`should correctly handle space in the beginning of phone number which could come from a + in prefill URL`, async ({}) => {+ const schema = getBookingResponsesSchema({+ bookingFields: [+ {+ name: "name",+ type: "name",+ required: true,+ },+ {+ name: "email",+ type: "email",+ required: true,+ },+ {+ name: "radioInput",+ type: "radioInput",+ required: true,+ optionsInputs: {+ // A field of type phone+ randomField: {+ type: "phone",+ },+ },+ },+ ] as z.infer<typeof eventTypeBookingFields> & z.BRAND<"HAS_SYSTEM_FIELDS">,+ view: "ALL_VIEWS",+ });+ const parsedResponses = await schema.safeParseAsync({+ email: "[email protected]",+ name: "test",+ radioInput: JSON.stringify({+ value: "randomField",+ optionValue: " 919999999999",+ }),+ });+ expect(parsedResponses.success).toBe(true);+ if (!parsedResponses.success) {+ throw new Error("Should not reach here");+ }+ expect(parsedResponses.data).toEqual({+ email: "[email protected]",+ name: "test",+ radioInput: {+ value: "randomField",+ optionValue: "+919999999999",+ },+ });+ });+});+describe("validate url type field", () => {test(`should fail parsing if invalid url provided`, async ({}) => {const schema = getBookingResponsesSchema({diff --git a/packages/features/bookings/lib/getBookingResponsesSchema.ts b/packages/features/bookings/lib/getBookingResponsesSchema.tsindex 31b3c732f09..6260bbc1265 100644--- a/packages/features/bookings/lib/getBookingResponsesSchema.ts+++ b/packages/features/bookings/lib/getBookingResponsesSchema.ts@@ -15,6 +15,11 @@ export const bookingResponsesDbSchema = z.record(dbReadResponseSchema);const catchAllSchema = bookingResponsesDbSchema;+const ensureValidPhoneNumber = (value: string) => {+ // + 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+ return value.replace(/^ +/, "+");+};export const getBookingResponsesPartialSchema = ({ bookingFields, view }: CommonParams) => {const schema = bookingResponses.unwrap().partial().and(catchAllSchema);@@ -89,11 +94,14 @@ function preprocess<T extends z.ZodType>({try {parsedValue = JSON.parse(value);} catch (e) {}+ const optionsInputs = field.optionsInputs;+ const optionInputField = optionsInputs?.[parsedValue.value];+ if (optionInputField && optionInputField.type === "phone") {+ parsedValue.optionValue = ensureValidPhoneNumber(parsedValue.optionValue);+ }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(/^ +/, "+");+ newResponses[field.name] = ensureValidPhoneNumber(value);} else {newResponses[field.name] = value;}