From 80e741e82ac6fa4677259da7e0180654254914a1 Mon Sep 17 00:00:00 2001 From: Hariom Balhara Date: Thu, 21 Sep 2023 17:19:46 +0530 Subject: [PATCH] Fix prefill failing if name isnt provided --- packages/features/bookings/lib/getBookingResponsesSchema.ts | 4 ++-- packages/features/form-builder/schema.ts | 5 +++-- packages/prisma/zod-utils.ts | 5 +++-- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/packages/features/bookings/lib/getBookingResponsesSchema.ts b/packages/features/bookings/lib/getBookingResponsesSchema.ts index 067b5e1b11..2f8242019d 100644 --- a/packages/features/bookings/lib/getBookingResponsesSchema.ts +++ b/packages/features/bookings/lib/getBookingResponsesSchema.ts @@ -254,8 +254,8 @@ function preprocess({ ); if (isPartialSchema) { // Query Params can be completely invalid, try to preprocess as much of it in correct format but in worst case simply don't prefill instead of crashing - return preprocessed.catch(() => { - console.error("Failed to preprocess query params, prefilling will be skipped"); + return preprocessed.catch(function (res?: { error?: unknown[] }) { + console.error("Failed to preprocess query params, prefilling will be skipped", res?.error); return {}; }); } diff --git a/packages/features/form-builder/schema.ts b/packages/features/form-builder/schema.ts index f873cd818a..b511a15afe 100644 --- a/packages/features/form-builder/schema.ts +++ b/packages/features/form-builder/schema.ts @@ -1,6 +1,7 @@ import { z } from "zod"; import { getValidRhfFieldName } from "@calcom/lib/getValidRhfFieldName"; +import { nonEmptyString } from "@calcom/prisma/zod-utils"; import { fieldTypesConfigMap } from "./fieldTypes"; import { getVariantsConfig, preprocessNameFieldDataWithVariant } from "./utils"; @@ -289,7 +290,7 @@ export const fieldTypesSchemaMap: Partial< if (fields.length === 1) { const field = fields[0]; if (variantSupportedFields.includes(field.type)) { - const schema = stringSchema; + const schema = field.required ? nonEmptyString : stringSchema; if (!schema.safeParse(response).success) { ctx.addIssue({ code: z.ZodIssueCode.custom, message: m("Invalid string") }); } @@ -299,7 +300,7 @@ export const fieldTypesSchemaMap: Partial< } } fields.forEach((subField) => { - const schema = stringSchema; + const schema = subField.required ? nonEmptyString : stringSchema; if (!variantSupportedFields.includes(subField.type)) { throw new Error(`Unsupported field.type with variants: ${subField.type}`); } diff --git a/packages/prisma/zod-utils.ts b/packages/prisma/zod-utils.ts index d3fb0b9e31..544707b5cb 100644 --- a/packages/prisma/zod-utils.ts +++ b/packages/prisma/zod-utils.ts @@ -115,14 +115,15 @@ export type BookingFieldType = FormBuilderFieldType; // Validation of user added bookingFields' responses happen using `getBookingResponsesSchema` which requires `eventType`. // So it is a dynamic validation and thus entire validation can't exist here +// Note that this validation runs to validate prefill params as well, so it should consider that partial values can be there. e.g. `name` might be empty string export const bookingResponses = z .object({ email: z.string(), //TODO: Why don't we move name out of bookingResponses and let it be handled like user fields? name: z.union([ - nonEmptyString(), + z.string(), z.object({ - firstName: nonEmptyString(), + firstName: z.string(), lastName: z.string().optional(), }), ]),