From 5cfef63e079248d4501174ce48b2d4709dc17c56 Mon Sep 17 00:00:00 2001 From: Hongbo Wu Date: Thu, 4 Apr 2024 11:57:30 +0800 Subject: [PATCH] add database id validation and normalization --- .../pages/settings/integrations/notion.tsx | 60 +++++++++++++------ 1 file changed, 43 insertions(+), 17 deletions(-) diff --git a/packages/web/pages/settings/integrations/notion.tsx b/packages/web/pages/settings/integrations/notion.tsx index a8760da8e..6a402deaa 100644 --- a/packages/web/pages/settings/integrations/notion.tsx +++ b/packages/web/pages/settings/integrations/notion.tsx @@ -30,8 +30,7 @@ import { applyStoredTheme } from '../../../lib/themeUpdater' import { showSuccessToast } from '../../../lib/toastHelpers' type FieldType = { - // parentPageId?: string - parentDatabaseId?: string + parentDatabaseId: string properties?: string[] } @@ -47,7 +46,6 @@ export default function Notion(): JSX.Element { useEffect(() => { form.setFieldsValue({ - // parentPageId: notion.settings?.parentPageId, parentDatabaseId: notion.settings?.parentDatabaseId, properties: notion.settings?.properties, }) @@ -72,6 +70,28 @@ export default function Notion(): JSX.Element { }) } + const normalizeDatabaseId = useCallback( + (value: string) => { + // check if database id is in UUIDv4 format + const uuidRegex = + /^[0-9a-fA-F]{8}[0-9a-fA-F]{4}[0-9a-fA-F]{4}[0-9a-fA-F]{4}[0-9a-fA-F]{12}$/ + if (uuidRegex.test(value)) { + return value + } + + // extract the database id from the URL + // https://www.notion.so/ec460c235baa4da5bb412971a12e9dbe?v=8f4e324c0b584b67b8b7cfe9a2f996d7 -> ec460c235baa4da5bb412971a12e9dbe + const urlRegex = /https:\/\/www.notion.so\/([a-f0-9]{32})\?*/ + const match = value.match(urlRegex) + if (!match || match.length < 2) { + messageApi.error('Invalid Notion Database ID.') + return value + } + return match[1] + }, + [messageApi] + ) + const onFinish: FormProps['onFinish'] = async (values) => { try { await updateNotion(values) @@ -167,29 +187,35 @@ export default function Notion(): JSX.Element { onFinish={onFinish} onFinishFailed={onFinishFailed} > - {/* - label="Notion Page Id" - name="parentPageId" - help="The id of the Notion page where the items will be exported to. You can find it in the URL of the page." - rules={[ - { - required: true, - message: 'Please input your Notion Page Id!', - }, - ]} - > - - */} - label="Notion Database ID" name="parentDatabaseId" help="The ID of the Notion database where the items will be exported to. You can find it in the URL of the database." + normalize={normalizeDatabaseId} rules={[ { required: true, message: 'Please input your Notion Database ID!', }, + { + validator: (_, value) => { + // check if database id is in UUIDv4 format + const uuidRegex = /^[0-9a-fA-F]{8}[0-9a-fA-F]{4}[0-9a-fA-F]{4}[0-9a-fA-F]{4}[0-9a-fA-F]{12}$/ + if (uuidRegex.test(value)) { + return Promise.resolve() + } + // extract the database id from the URL + const urlRegex = + /https:\/\/www.notion.so\/([a-f0-9]{32})\?*/ + const match = value.match(urlRegex) + if (match && match.length >= 2) { + return Promise.resolve() + } + return Promise.reject( + new Error('Invalid Notion Database ID.') + ) + }, + }, ]} >