This commit is contained in:
dengqichen 2025-01-20 15:34:27 +08:00
parent 10d1e1f6c2
commit 066be11a90

View File

@ -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<typeof formSchema>;
interface DeploymentConfigModalProps {
open: boolean;
@ -80,13 +100,14 @@ const DeploymentConfigModal: React.FC<DeploymentConfigModalProps> = ({open, onCa
const [isFullscreen, setIsFullscreen] = useState(false);
const form = useForm<FormValues>({
resolver: zodResolver(formSchema),
defaultValues: {
applicationId: undefined,
externalSystemId: undefined,
jenkinsViewId: undefined,
jenkinsJobId: undefined,
script: '',
envs: [],
envs: []
}
});
@ -343,7 +364,16 @@ const DeploymentConfigModal: React.FC<DeploymentConfigModalProps> = ({open, onCa
name="script"
render={({field}) => (
<FormItem className="relative">
<div className="flex items-center justify-between mb-2">
<FormLabel></FormLabel>
<Button
type="button"
variant="outline"
size="sm"
>
</Button>
</div>
<FormControl>
<div className="relative">
<Button
@ -355,14 +385,6 @@ const DeploymentConfigModal: React.FC<DeploymentConfigModalProps> = ({open, onCa
>
<Maximize2 className="h-4 w-4"/>
</Button>
<Button
type="button"
variant="outline"
size="sm"
className="absolute right-14 top-2 z-10"
>
</Button>
<Editor
height="300px"
language="groovy"