fix: #15967 Show better error when wrong variable is used in custom event name (#15974)

* update to show exact err msg with custom event name

* updated tests for validateCustomEventName

---------

Co-authored-by: Keith Williams <keithwillcode@gmail.com>
Co-authored-by: Anik Dhabal Babu <81948346+anikdhabal@users.noreply.github.com>
This commit is contained in:
authored and GitHub committed 2024-07-29 20:23:53 +03:00
1 parent 9a1a86ed3a
commit 27d331ad2d
5 files changed
+24 -22

No files matched your search

+7 -7
View File
@@ -356,7 +356,7 @@ describe("event tests", () => {
describe("fn: validateCustomEventName", () => {
it("should be valid when no variables used", () => {
expect(event.validateCustomEventName("foo", "error message")).toBe(true);
expect(event.validateCustomEventName("foo")).toBe(true);
});
[
@@ -371,24 +371,24 @@ describe("event tests", () => {
"USER",
].forEach((value) => {
it(`should support {${value}} variable`, () => {
expect(event.validateCustomEventName(`foo {${value}} bar`, "error message")).toBe(true);
expect(event.validateCustomEventName(`foo {${value}} bar`)).toBe(true);
expect(event.validateCustomEventName(`{${value}} bar`, "error message")).toBe(true);
expect(event.validateCustomEventName(`{${value}} bar`)).toBe(true);
expect(event.validateCustomEventName(`foo {${value}}`, "error message")).toBe(true);
expect(event.validateCustomEventName(`foo {${value}}`)).toBe(true);
});
});
it("should support booking field variables", () => {
expect(
event.validateCustomEventName("foo{customField}bar", "error message", {
event.validateCustomEventName("foo{customField}bar", {
customField: true,
})
).toBe(true);
});
it("should return error when invalid variable used", () => {
expect(event.validateCustomEventName("foo{nonsenseField}bar", "error message")).toBe("error message");
it("should return variable when invalid variable used", () => {
expect(event.validateCustomEventName("foo{nonsenseField}bar")).toBe("{nonsenseField}");
});
});
});
+2 -6
View File
@@ -100,11 +100,7 @@ export function getEventName(eventNameObj: EventNameObjectType, forAttendeeView
return dynamicEventName;
}
export const validateCustomEventName = (
value: string,
message: string,
bookingFields?: Prisma.JsonObject
) => {
export const validateCustomEventName = (value: string, bookingFields?: Prisma.JsonObject) => {
let customInputVariables: string[] = [];
if (bookingFields) {
customInputVariables = Object.keys(bookingFields).map((customInput) => {
@@ -131,7 +127,7 @@ export const validateCustomEventName = (
if (matches?.length) {
for (const item of matches) {
if (!validVariables.includes(item)) {
return message;
return item;
}
}
}