fix: handle JSON string name field from URL prefill for firstAndLastName variant (#28039)
* fix: handle JSON string name field from URL prefill for firstAndLastName variant * refactoring logic * improve test coverage * remove unnecessary comment --------- Co-authored-by: Maks Pikov <mixelburg@users.noreply.github.com> Co-authored-by: Romit <romitgabani1.work@gmail.com> Co-authored-by: Romit <85230081+romitg2@users.noreply.github.com>
This commit is contained in:
2 files changed
+134
-2
No files matched your search
@@ -329,8 +329,7 @@ describe("getBookingResponsesSchema", () => {
|
||||
);
|
||||
});
|
||||
|
||||
// TODO: Fix this behaviour later
|
||||
test.skip("`firstAndLastName` variant to `fullName` when passed as JSON string from URL prefill", async () => {
|
||||
test("`firstAndLastName` variant to `fullName` when passed as JSON string from URL prefill", async () => {
|
||||
const schema = getBookingResponsesSchema({
|
||||
bookingFields: [
|
||||
{
|
||||
@@ -366,6 +365,126 @@ describe("getBookingResponsesSchema", () => {
|
||||
);
|
||||
});
|
||||
|
||||
test("JSON string with only firstName (no lastName) from URL prefill", async () => {
|
||||
const schema = getBookingResponsesSchema({
|
||||
bookingFields: [
|
||||
{
|
||||
name: "name",
|
||||
type: "name",
|
||||
required: true,
|
||||
},
|
||||
{
|
||||
name: "email",
|
||||
type: "email",
|
||||
required: true,
|
||||
},
|
||||
] as z.infer<typeof eventTypeBookingFields> & z.BRAND<"HAS_SYSTEM_FIELDS">,
|
||||
view: "ALL_VIEWS",
|
||||
});
|
||||
const parsedResponses = await schema.safeParseAsync({
|
||||
name: '{"firstName":"John"}',
|
||||
email: "john@example.com",
|
||||
});
|
||||
|
||||
expectResponsesToBe(
|
||||
parsedResponses,
|
||||
expect.objectContaining({
|
||||
name: "John",
|
||||
email: "john@example.com",
|
||||
})
|
||||
);
|
||||
});
|
||||
|
||||
test("Invalid JSON string falls back to regular string handling", async () => {
|
||||
const schema = getBookingResponsesSchema({
|
||||
bookingFields: [
|
||||
{
|
||||
name: "name",
|
||||
type: "name",
|
||||
required: true,
|
||||
},
|
||||
{
|
||||
name: "email",
|
||||
type: "email",
|
||||
required: true,
|
||||
},
|
||||
] as z.infer<typeof eventTypeBookingFields> & z.BRAND<"HAS_SYSTEM_FIELDS">,
|
||||
view: "ALL_VIEWS",
|
||||
});
|
||||
const parsedResponses = await schema.safeParseAsync({
|
||||
name: "{not valid json}",
|
||||
email: "john@example.com",
|
||||
});
|
||||
|
||||
expectResponsesToBe(
|
||||
parsedResponses,
|
||||
expect.objectContaining({
|
||||
name: "{not valid json}",
|
||||
email: "john@example.com",
|
||||
})
|
||||
);
|
||||
});
|
||||
|
||||
test("JSON array falls back to regular string handling", async () => {
|
||||
const schema = getBookingResponsesSchema({
|
||||
bookingFields: [
|
||||
{
|
||||
name: "name",
|
||||
type: "name",
|
||||
required: true,
|
||||
},
|
||||
{
|
||||
name: "email",
|
||||
type: "email",
|
||||
required: true,
|
||||
},
|
||||
] as z.infer<typeof eventTypeBookingFields> & z.BRAND<"HAS_SYSTEM_FIELDS">,
|
||||
view: "ALL_VIEWS",
|
||||
});
|
||||
const parsedResponses = await schema.safeParseAsync({
|
||||
name: '["John", "Doe"]',
|
||||
email: "john@example.com",
|
||||
});
|
||||
|
||||
expectResponsesToBe(
|
||||
parsedResponses,
|
||||
expect.objectContaining({
|
||||
name: '["John", "Doe"]',
|
||||
email: "john@example.com",
|
||||
})
|
||||
);
|
||||
});
|
||||
|
||||
test("JSON object without firstName falls back to regular string handling", async () => {
|
||||
const schema = getBookingResponsesSchema({
|
||||
bookingFields: [
|
||||
{
|
||||
name: "name",
|
||||
type: "name",
|
||||
required: true,
|
||||
},
|
||||
{
|
||||
name: "email",
|
||||
type: "email",
|
||||
required: true,
|
||||
},
|
||||
] as z.infer<typeof eventTypeBookingFields> & z.BRAND<"HAS_SYSTEM_FIELDS">,
|
||||
view: "ALL_VIEWS",
|
||||
});
|
||||
const parsedResponses = await schema.safeParseAsync({
|
||||
name: '{"name":"John Doe"}',
|
||||
email: "john@example.com",
|
||||
});
|
||||
|
||||
expectResponsesToBe(
|
||||
parsedResponses,
|
||||
expect.objectContaining({
|
||||
name: '{"name":"John Doe"}',
|
||||
email: "john@example.com",
|
||||
})
|
||||
);
|
||||
});
|
||||
|
||||
test("`fullName` to `firstAndLastName` when there is a lastName(separated by space)", async () => {
|
||||
const schema = getBookingResponsesSchema({
|
||||
bookingFields: [
|
||||
|
||||
@@ -144,6 +144,19 @@ export const fieldTypesSchemaMap = {
|
||||
}
|
||||
|
||||
if (typeof response === "string") {
|
||||
const nameJsonSchema = z.object({
|
||||
firstName: z.string(),
|
||||
lastName: z.string().optional().default(""),
|
||||
});
|
||||
|
||||
try {
|
||||
const parsed = nameJsonSchema.safeParse(JSON.parse(response));
|
||||
if (parsed.success) {
|
||||
return preprocessNameFieldDataWithVariant(correctedVariant, parsed.data);
|
||||
}
|
||||
} catch {
|
||||
// if invalid JSON, then treat as regular string
|
||||
}
|
||||
return preprocessNameFieldDataWithVariant(correctedVariant, response);
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user