From 2da2552c3bf7091f81f20ff5852fd4416ff90ce9 Mon Sep 17 00:00:00 2001 From: Amit Ranjan Date: Tue, 24 Sep 2024 02:21:02 +0530 Subject: [PATCH] fix: improve error handling for invalid URL submission --- frontend/src/components/Menu/MenuDialog.tsx | 4 +++- frontend/src/constants.ts | 3 +++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/frontend/src/components/Menu/MenuDialog.tsx b/frontend/src/components/Menu/MenuDialog.tsx index 936a23b..f5463db 100644 --- a/frontend/src/components/Menu/MenuDialog.tsx +++ b/frontend/src/components/Menu/MenuDialog.tsx @@ -25,6 +25,7 @@ import { ContentContainer } from "@/app-components/dialogs/layouts/ContentContai import { ContentItem } from "@/app-components/dialogs/layouts/ContentItem"; import { Input } from "@/app-components/inputs/Input"; import { ToggleableInput } from "@/app-components/inputs/ToggleableInput"; +import { URL_REGEX } from "@/constants"; import { IMenuItem, IMenuItemAttributes, MenuType } from "@/types/menu.types"; export type MenuDialogProps = DialogProps & { @@ -66,7 +67,8 @@ export const MenuDialog: FC = ({ url: { required: t("message.url_is_invalid"), validate: (value: string = "") => - isAbsoluteUrl(value) || t("message.url_is_invalid"), + (isAbsoluteUrl(value) && URL_REGEX.test(value)) || + t("message.url_is_invalid"), }, payload: {}, }; diff --git a/frontend/src/constants.ts b/frontend/src/constants.ts index 6e9d737..17d93ab 100644 --- a/frontend/src/constants.ts +++ b/frontend/src/constants.ts @@ -21,3 +21,6 @@ export const DATE_TIME_FORMAT = { export const USER_DEFAULT_PICTURE = "https://avatars.dicebear.com/v2/identicon/6659e07058af581e68e33d05.svg"; + +export const URL_REGEX = + /^(https?:\/\/)?((([a-zA-Z0-9-]+\.)+[a-zA-Z]{2,})(:[0-9]{1,5})?(\/[^\s]*)?)$/i;