feat:Add more constraints for server name, server ID, and api endpoint.

This commit is contained in:
riverLethe committed 2025-12-08 09:08:09 +08:00
1 parent 54654b386d
commit 96db00418c
4 files changed
+74 -19

No files matched your search

+6 -1
View File
@@ -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"
}
@@ -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": "简短描述为必填项"
}
@@ -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<string, string> = {};
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<ServiceEditPageProps> = ({
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<ServiceEditPageProps> = ({
onInputChange={handleInputChange}
onAddTag={handleAddTag}
onRemoveTag={handleRemoveTag}
errors={validationErrors}
/>
</div>
<div className="w-[400px] max-w-[1/2]">
@@ -13,6 +13,7 @@ interface BasicInfoTabProps {
onInputChange: (field: keyof MCPServiceFormData, value: any) => void;
onAddTag: () => void;
onRemoveTag: (tag: string) => void;
errors?: Record<string, string>;
}
export const BasicInfoTab: React.FC<BasicInfoTabProps> = ({
@@ -22,6 +23,7 @@ export const BasicInfoTab: React.FC<BasicInfoTabProps> = ({
onInputChange,
onAddTag,
onRemoveTag,
errors,
}) => {
const { t } = useTranslation();
@@ -41,7 +43,9 @@ export const BasicInfoTab: React.FC<BasicInfoTabProps> = ({
)}
labelPlacement="outside"
isRequired
errorMessage={t("Server ID is required")}
maxLength={256}
isInvalid={!!errors?.slug_name}
errorMessage={errors?.slug_name || t("Server ID is required")}
/>
<Input
label={t("Server Name")}
@@ -50,7 +54,9 @@ export const BasicInfoTab: React.FC<BasicInfoTabProps> = ({
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")}
/>
<Input
label={t("Short Description")}
@@ -59,7 +65,10 @@ export const BasicInfoTab: React.FC<BasicInfoTabProps> = ({
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")
}
/>
<Input
@@ -69,7 +78,8 @@ export const BasicInfoTab: React.FC<BasicInfoTabProps> = ({
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 */}
<HeadersManagementSettings