From 066be11a90e5cc7a10d230d0ae7e2f3b16ec78d5 Mon Sep 17 00:00:00 2001 From: dengqichen Date: Mon, 20 Jan 2025 15:34:27 +0800 Subject: [PATCH] 1 --- .../List/components/DeploymentConfigModal.tsx | 64 +++++++++++++------ 1 file changed, 43 insertions(+), 21 deletions(-) diff --git a/frontend/src/pages/Deploy/Deployment/List/components/DeploymentConfigModal.tsx b/frontend/src/pages/Deploy/Deployment/List/components/DeploymentConfigModal.tsx index 5b5069d4..1a35c583 100644 --- a/frontend/src/pages/Deploy/Deployment/List/components/DeploymentConfigModal.tsx +++ b/frontend/src/pages/Deploy/Deployment/List/components/DeploymentConfigModal.tsx @@ -1,3 +1,5 @@ +import * as z from "zod"; +import { zodResolver } from '@hookform/resolvers/zod'; import React, {useEffect, useState} from 'react'; import { Dialog, @@ -53,17 +55,35 @@ interface EnvVariable { value: string; } -interface FormValues { - applicationId?: number; - externalSystemId?: number; - jenkinsViewId?: number; - jenkinsJobId?: number; - script: string; - envs: { - key: string; - value: string; - }[]; -} +const envKeySchema = z.string() + .refine(val => { + // 不能为空 + if (!val) return false; + // 不能以数字开头 + if (/^\d/.test(val)) return false; + // 不能以符号开头 + if (/^[^a-zA-Z]/.test(val)) return false; + // 不能是纯数字 + if (/^\d+$/.test(val)) return false; + // 不能是纯中文 + if (/^[\u4e00-\u9fa5]+$/.test(val)) return false; + // 只允许字母、数字和下划线 + return /^[a-zA-Z][a-zA-Z0-9_]*$/.test(val); + }, "变量名只能以字母开头,且只能包含字母、数字和下划线"); + +const formSchema = z.object({ + applicationId: z.number().optional(), + externalSystemId: z.number().optional(), + jenkinsViewId: z.number().optional(), + jenkinsJobId: z.number().optional(), + script: z.string(), + envs: z.array(z.object({ + key: envKeySchema, + value: z.string() + })) +}); + +type FormValues = z.infer; interface DeploymentConfigModalProps { open: boolean; @@ -80,13 +100,14 @@ const DeploymentConfigModal: React.FC = ({open, onCa const [isFullscreen, setIsFullscreen] = useState(false); const form = useForm({ + resolver: zodResolver(formSchema), defaultValues: { applicationId: undefined, externalSystemId: undefined, jenkinsViewId: undefined, jenkinsJobId: undefined, script: '', - envs: [], + envs: [] } }); @@ -343,7 +364,16 @@ const DeploymentConfigModal: React.FC = ({open, onCa name="script" render={({field}) => ( - 部署脚本 +
+ 部署脚本 + +
-