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) {
// 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 {};
});
}

View File

@ -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}`);
}

View File

@ -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(),
}),
]),