From 96db00418c906392f5bc7f13265c6e1030e37991 Mon Sep 17 00:00:00 2001 From: riverLethe <865192079@qq.com> Date: Mon, 8 Dec 2025 09:08:09 +0800 Subject: [PATCH] feat:Add more constraints for server name, server ID, and api endpoint. --- frontend/public/locales/en/translation.json | 7 ++- .../public/locales/zh-CN/translation.json | 7 ++- .../mcp-services/ServiceEditPage.tsx | 61 +++++++++++++++---- .../components/mcp-services/BasicInfoTab.tsx | 18 ++++-- 4 files changed, 74 insertions(+), 19 deletions(-) diff --git a/frontend/public/locales/en/translation.json b/frontend/public/locales/en/translation.json index 0d2094d..5c8c300 100644 --- a/frontend/public/locales/en/translation.json +++ b/frontend/public/locales/en/translation.json @@ -876,5 +876,10 @@ "Email is required": "Email is required", "No servers available": "No servers available", "(Deleted)": "(Deleted)", - "Transport Type": "Transport Type" + "Transport Type": "Transport Type", + "Server ID must be less than 256 characters": "Server ID must be less than 256 characters", + "Server Name must be less than 256 characters": "Server Name must be less than 256 characters", + "API Endpoint must be a valid HTTP/HTTPS URL": "API Endpoint must be a valid HTTP/HTTPS URL", + "Server ID is required": "Server ID is required", + "Short description is required": "Short description is required" } diff --git a/frontend/public/locales/zh-CN/translation.json b/frontend/public/locales/zh-CN/translation.json index 601b4bf..6107c7b 100644 --- a/frontend/public/locales/zh-CN/translation.json +++ b/frontend/public/locales/zh-CN/translation.json @@ -878,5 +878,10 @@ "Email is required": "邮箱为必填项", "No servers available": "没有可用服务", "(Deleted)": "(已删除)", - "Transport Type": "传输方式" + "Transport Type": "传输方式", + "Server ID must be less than 256 characters": "Server ID 必须少于256个字符", + "Server Name must be less than 256 characters": "Server Name 必须少于256个字符", + "API Endpoint must be a valid HTTP/HTTPS URL": "API Endpoint 必须是一个有效的HTTP/HTTPS URL", + "Server ID is required": "Server ID 为必填项", + "Short description is required": "简短描述为必填项" } diff --git a/frontend/src/components/mcp-services/ServiceEditPage.tsx b/frontend/src/components/mcp-services/ServiceEditPage.tsx index 8cea115..2ed5568 100644 --- a/frontend/src/components/mcp-services/ServiceEditPage.tsx +++ b/frontend/src/components/mcp-services/ServiceEditPage.tsx @@ -34,6 +34,45 @@ const _DefaultFormData: MCPServiceFormData = { apis: [], }; +// Helper function to validate URL +const isValidUrl = (url: string) => { + if (!url) return false; + try { + const parsed = new URL(url); + return parsed.protocol === "http:" || parsed.protocol === "https:"; + } catch { + return false; + } +}; + +const validateServiceForm = (data: MCPServiceFormData, t: any) => { + const errors: Record = {}; + + if (!data.slug_name?.trim()) { + errors.slug_name = t("Server ID is required"); + } else if (data.slug_name.length > 256) { + errors.slug_name = t("Server ID must be less than 256 characters"); + } + + if (!data.name?.trim()) { + errors.name = t("Server name is required"); + } else if (data.name.length > 256) { + errors.name = t("Server Name must be less than 256 characters"); + } + + if (!data.base_url?.trim()) { + errors.base_url = t("API Endpoint is required"); + } else if (!isValidUrl(data.base_url)) { + errors.base_url = t("API Endpoint must be a valid HTTP/HTTPS URL"); + } + + if (!data.short_description?.trim()) { + errors.short_description = t("Short description is required"); + } + + return errors; +}; + interface ServiceEditPageProps { serviceId?: string; onSave: (data: MCPServiceFormData, isDraft?: boolean) => void; @@ -202,37 +241,32 @@ const BaseServiceEditPage: React.FC = ({ return !isNaN(numValue) && numValue >= 0; }; + const validationErrors = validateServiceForm(formData, t); + const isFormValid = () => { - const hasBasicInfo = - formData.name.trim() && - formData.short_description?.trim() && - formData.slug_name?.trim() && - formData.base_url?.trim(); + if (Object.keys(validationErrors).length > 0) { + return false; + } // if free, no need to validate price if (formData.charge_type === ChargeType.Free) { - return hasBasicInfo; + return true; } // if PerCall, validate price if (formData.charge_type === ChargeType.PerCall) { - return hasBasicInfo && isValidPrice(formData.price); + return isValidPrice(formData.price); } // if PerToken, validate both input and output token prices if (formData.charge_type === ChargeType.PerToken) { return ( - hasBasicInfo && isValidPrice(formData.input_token_price) && isValidPrice(formData.output_token_price) ); } // if paid, need to validate price - return ( - hasBasicInfo && - formData.price !== undefined && - formData.price.trim() !== "" - ); + return formData.price !== undefined && formData.price.trim() !== ""; }; return ( @@ -321,6 +355,7 @@ const BaseServiceEditPage: React.FC = ({ onInputChange={handleInputChange} onAddTag={handleAddTag} onRemoveTag={handleRemoveTag} + errors={validationErrors} />
diff --git a/frontend/src/shared/components/mcp-services/BasicInfoTab.tsx b/frontend/src/shared/components/mcp-services/BasicInfoTab.tsx index c30eee4..c239c37 100644 --- a/frontend/src/shared/components/mcp-services/BasicInfoTab.tsx +++ b/frontend/src/shared/components/mcp-services/BasicInfoTab.tsx @@ -13,6 +13,7 @@ interface BasicInfoTabProps { onInputChange: (field: keyof MCPServiceFormData, value: any) => void; onAddTag: () => void; onRemoveTag: (tag: string) => void; + errors?: Record; } export const BasicInfoTab: React.FC = ({ @@ -22,6 +23,7 @@ export const BasicInfoTab: React.FC = ({ onInputChange, onAddTag, onRemoveTag, + errors, }) => { const { t } = useTranslation(); @@ -41,7 +43,9 @@ export const BasicInfoTab: React.FC = ({ )} labelPlacement="outside" isRequired - errorMessage={t("Server ID is required")} + maxLength={256} + isInvalid={!!errors?.slug_name} + errorMessage={errors?.slug_name || t("Server ID is required")} /> = ({ onChange={(e) => onInputChange("name", e.target.value)} isRequired labelPlacement="outside" - errorMessage={t("Server name is required")} + maxLength={256} + isInvalid={!!errors?.name} + errorMessage={errors?.name || t("Server name is required")} /> = ({ onChange={(e) => onInputChange("short_description", e.target.value)} isRequired labelPlacement="outside" - errorMessage={t("Short description is required")} + isInvalid={!!errors?.short_description} + errorMessage={ + errors?.short_description || t("Short description is required") + } /> = ({ onChange={(e) => onInputChange("base_url", e.target.value)} isRequired labelPlacement="outside" - errorMessage={t("API Endpoint is required")} + isInvalid={!!errors?.base_url} + errorMessage={errors?.base_url || t("API Endpoint is required")} /> {/* headers input fields */}