From 36689676a3c46c7f470b91a374d75ef403a86c7b Mon Sep 17 00:00:00 2001 From: Lei OT Date: Fri, 23 Jan 2026 19:43:30 +0800 Subject: [PATCH] feat(commons): url validate --- src/utils/commons.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/utils/commons.js b/src/utils/commons.js index 5c77f32..c95ee2c 100644 --- a/src/utils/commons.js +++ b/src/utils/commons.js @@ -922,3 +922,16 @@ export const buildTree = (list, keyMap = { rootKeys: [], ignoreKeys: [] }) => { return treeRoots; }; + +export const isValidUrl = (str) => { + if (typeof str !== "string") return false; + + try { + // If no protocol, prepend "http://" + const hasProtocol = /^[a-zA-Z][a-zA-Z\d+\-.]*:\/\//.test(str); + new URL(hasProtocol ? str : `http://${str}`); + return true; + } catch { + return false; + } +}