Fix prefill failing if name isnt provided

fix/prefill-not-working-if-name-not-provided
Hariom Balhara 2023-09-21 17:19:46 +05:30
parent 437a44060b
commit 80e741e82a
3 changed files with 8 additions and 6 deletions

View File

@ -254,8 +254,8 @@ function preprocess<T extends z.ZodType>({
); );
if (isPartialSchema) { 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 // 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(() => { return preprocessed.catch(function (res?: { error?: unknown[] }) {
console.error("Failed to preprocess query params, prefilling will be skipped"); console.error("Failed to preprocess query params, prefilling will be skipped", res?.error);
return {}; return {};
}); });
} }

View File

@ -1,6 +1,7 @@
import { z } from "zod"; import { z } from "zod";
import { getValidRhfFieldName } from "@calcom/lib/getValidRhfFieldName"; import { getValidRhfFieldName } from "@calcom/lib/getValidRhfFieldName";
import { nonEmptyString } from "@calcom/prisma/zod-utils";
import { fieldTypesConfigMap } from "./fieldTypes"; import { fieldTypesConfigMap } from "./fieldTypes";
import { getVariantsConfig, preprocessNameFieldDataWithVariant } from "./utils"; import { getVariantsConfig, preprocessNameFieldDataWithVariant } from "./utils";
@ -289,7 +290,7 @@ export const fieldTypesSchemaMap: Partial<
if (fields.length === 1) { if (fields.length === 1) {
const field = fields[0]; const field = fields[0];
if (variantSupportedFields.includes(field.type)) { if (variantSupportedFields.includes(field.type)) {
const schema = stringSchema; const schema = field.required ? nonEmptyString : stringSchema;
if (!schema.safeParse(response).success) { if (!schema.safeParse(response).success) {
ctx.addIssue({ code: z.ZodIssueCode.custom, message: m("Invalid string") }); ctx.addIssue({ code: z.ZodIssueCode.custom, message: m("Invalid string") });
} }
@ -299,7 +300,7 @@ export const fieldTypesSchemaMap: Partial<
} }
} }
fields.forEach((subField) => { fields.forEach((subField) => {
const schema = stringSchema; const schema = subField.required ? nonEmptyString : stringSchema;
if (!variantSupportedFields.includes(subField.type)) { if (!variantSupportedFields.includes(subField.type)) {
throw new Error(`Unsupported field.type with variants: ${subField.type}`); throw new Error(`Unsupported field.type with variants: ${subField.type}`);
} }

View File

@ -115,14 +115,15 @@ export type BookingFieldType = FormBuilderFieldType;
// Validation of user added bookingFields' responses happen using `getBookingResponsesSchema` which requires `eventType`. // 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 // 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 export const bookingResponses = z
.object({ .object({
email: z.string(), email: z.string(),
//TODO: Why don't we move name out of bookingResponses and let it be handled like user fields? //TODO: Why don't we move name out of bookingResponses and let it be handled like user fields?
name: z.union([ name: z.union([
nonEmptyString(), z.string(),
z.object({ z.object({
firstName: nonEmptyString(), firstName: z.string(),
lastName: z.string().optional(), lastName: z.string().optional(),
}), }),
]), ]),