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>
This commit is contained in:
co-authored by
Udit Takkar
parent
a559daa076
commit
bbb86f2174
@@ -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<typeof eventTypeBookingFields> & 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: [
|
||||
|
||||
@@ -90,6 +90,10 @@ function preprocess<T extends z.ZodType>({
|
||||
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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user