fix: Allow i18n of form builder max/min character count. Earlier strings missed due to variable. (#22367)

* added german translation

* fixed count issue

* fixed count issue

* resolved type safety errors

* fix

* Update getBookingResponsesSchema.ts

* fixes

* removed 1 character case

* fixes

* fix

* chore

* fix

* type-check

* type-fix

---------

Co-authored-by: Kartik Saini <[email protected]>
Co-authored-by: Volnei Munhoz <[email protected]>
Co-authored-by: Anik Dhabal Babu <[email protected]>
Co-authored-by: Dhairyashil Shinde <[email protected]>
Co-authored-by: Pallav <[email protected]>
This commit is contained in:
authored and GitHub committed 2025-12-02 02:24:54 +00:00
1 parent f359d37799
commit 5d8c4ad13d
4 files changed
+32 -11

No files matched your search

@@ -10,6 +10,8 @@
"accept_invitation": "Accept Invitation",
"max_characters": "Max. Characters",
"min_characters": "Min. Characters",
"min_characters_required": "Min. {{count}} characters required",
"max_characters_allowed": "Max. {{count}} characters allowed",
"calcom_explained": "{{appName}} provides scheduling infrastructure for absolutely everyone.",
"calcom_explained_new_user": "Finish setting up your {{appName}} account! You're just a few steps away from solving all your scheduling problems.",
"have_any_questions": "Have questions? We're here to help.",
@@ -48,6 +48,7 @@ export const useBookingForm = ({
? getBookingResponsesSchema({
bookingFields: event.bookingFields,
view: rescheduleUid ? "reschedule" : "booking",
translateFn: (key: string, options?: Record<string, unknown>) => t(key, options ?? {}),
})
: // Fallback until event is loaded.
z.object({}),
@@ -10,7 +10,8 @@ import { bookingResponses, emailSchemaRefinement } from "@calcom/prisma/zod-util
// eslint-disable-next-line @typescript-eslint/ban-types
type View = ALL_VIEWS | (string & {});
type BookingFields = (z.infer<typeof eventTypeBookingFields> & z.BRAND<"HAS_SYSTEM_FIELDS">) | null;
type CommonParams = { bookingFields: BookingFields; view: View };
type TranslationFunction = (key: string, options?: Record<string, unknown>) => string;
type CommonParams = { bookingFields: BookingFields; view: View; translateFn?: TranslationFunction };
export const bookingResponse = dbReadResponseSchema;
export const bookingResponsesDbSchema = z.record(dbReadResponseSchema);
@@ -23,24 +24,35 @@ const ensureValidPhoneNumber = (value: string) => {
// Replace the space(s) in the beginning with + as it is supposed to be provided in the beginning only
return value.replace(/^ +/, "+");
};
export const getBookingResponsesPartialSchema = ({ bookingFields, view }: CommonParams) => {
export const getBookingResponsesPartialSchema = ({ bookingFields, view, translateFn }: CommonParams) => {
const schema = bookingResponses.unwrap().partial().and(catchAllSchema);
return preprocess({ schema, bookingFields, isPartialSchema: true, view });
return preprocess({ schema, bookingFields, isPartialSchema: true, view, translateFn });
};
// Should be used when we know that not all fields responses are present
// - Can happen when we are parsing the prefill query string
// - Can happen when we are parsing a booking's responses (which was created before we added a new required field)
export default function getBookingResponsesSchema({ bookingFields, view }: CommonParams) {
export default function getBookingResponsesSchema({ bookingFields, view, translateFn }: CommonParams) {
const schema = bookingResponses.and(z.record(z.any()));
return preprocess({ schema, bookingFields, isPartialSchema: false, view });
return preprocess({ schema, bookingFields, isPartialSchema: false, view, translateFn });
}
// Should be used when we want to check if the optional fields are entered and valid as well
export function getBookingResponsesSchemaWithOptionalChecks({ bookingFields, view }: CommonParams) {
export function getBookingResponsesSchemaWithOptionalChecks({
bookingFields,
view,
translateFn,
}: CommonParams) {
const schema = bookingResponses.and(z.record(z.any()));
return preprocess({ schema, bookingFields, isPartialSchema: false, view, checkOptional: true });
return preprocess({
schema,
bookingFields,
isPartialSchema: false,
view,
checkOptional: true,
translateFn,
});
}
// TODO: Move preprocess of `booking.responses` to FormBuilder schema as that is going to parse the fields supported by FormBuilder
@@ -51,6 +63,7 @@ function preprocess<T extends z.ZodType>({
isPartialSchema,
view: currentView,
checkOptional = false,
translateFn,
}: CommonParams & {
schema: T;
// It is useful when we want to prefill the responses with the partial values. Partial can be in 2 ways
@@ -150,7 +163,10 @@ function preprocess<T extends z.ZodType>({
return isValidPhoneNumber(val);
});
// Tag the message with the input name so that the message can be shown at appropriate place
const m = (message: string) => `{${bookingField.name}}${message}`;
const m = (message: string, options?: Record<string, unknown>) => {
const translatedMessage = translateFn ? translateFn(message, options) : message;
return `{${bookingField.name}}${translatedMessage}`;
};
const views = bookingField.views;
const isFieldApplicableToCurrentView =
currentView === "ALL_VIEWS" ? true : views ? views.find((view) => view.id === currentView) : true;
+5 -3
View File
@@ -116,7 +116,7 @@ export const fieldTypesSchemaMap: Partial<
response: string;
isPartialSchema: boolean;
ctx: z.RefinementCtx;
m: (key: string) => string;
m: (key: string, options?: Record<string, unknown>) => string;
}) => void;
}
>
@@ -203,16 +203,18 @@ export const fieldTypesSchemaMap: Partial<
const hasExceededMaxLength = value.length > maxLength;
const hasNotReachedMinLength = value.length < minLength;
if (hasExceededMaxLength) {
const message = m(`max_characters_allowed`, { count: maxLength });
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: m(`Max. ${maxLength} characters allowed`),
message,
});
return;
}
if (hasNotReachedMinLength) {
const message = m(`min_characters_required`, { count: minLength });
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: m(`Min. ${minLength} characters required`),
message,
});
return;
}