/** * commands/datasource/_validate.ts — datasource create step machine (Phase D1) * * 镜像 web systemData/dataSource/Form.vue + 后端 DbLinkController * - fullName 必填 * - enCode 必填 * - dbType ∈ {MySQL, SqlServer, Oracle, DM, KingbaseES, PostgreSQL} * - host/port/userName/password 必填 * - DB001 镜像: 数据类型编码 * * v0.11e+ Phase D1: 剩余 40 命令 step machine 扩展 */ export interface ValidationError { field?: string message: string code?: string } export interface StepValidation { ok: boolean step: 0 stepName: string errors: ValidationError[] } const VALID_DB_TYPES = ['MySQL', 'SqlServer', 'Oracle', 'DM', 'KingbaseES', 'PostgreSQL'] export interface DatasourceOpts { fullName?: string enCode?: string dbType?: string host?: string port?: number | string userName?: string password?: string serviceName?: string enabledMark?: number | string } export function validateBasicInfo(opts: DatasourceOpts): StepValidation { const errors: ValidationError[] = [] if (!opts.fullName || !String(opts.fullName).trim()) { errors.push({ field: 'fullName', message: '数据源名称不能为空' }) } if (!opts.enCode || !String(opts.enCode).trim()) { errors.push({ field: 'enCode', message: '数据源编码不能为空' }) } if (!opts.dbType || !String(opts.dbType).trim()) { errors.push({ field: 'dbType', message: '数据库类型不能为空' }) } else if (!VALID_DB_TYPES.includes(String(opts.dbType))) { errors.push({ field: 'dbType', message: `数据库类型必须是 ${VALID_DB_TYPES.join('/')} 之一 (DB001 镜像), 当前 ${opts.dbType}`, code: 'DB001', }) } if (!opts.host || !String(opts.host).trim()) { errors.push({ field: 'host', message: '主机不能为空' }) } if (opts.port === undefined || opts.port === null) { errors.push({ field: 'port', message: '端口不能为空' }) } else { const port = Number(opts.port) if (isNaN(port) || port < 1 || port > 65535) { errors.push({ field: 'port', message: '端口必须是 1-65535 之间的数字' }) } } if (!opts.userName || !String(opts.userName).trim()) { errors.push({ field: 'userName', message: '用户名不能为空' }) } if (!opts.password || !String(opts.password).trim()) { errors.push({ field: 'password', message: '密码不能为空' }) } if (opts.enabledMark !== undefined && ![0, 1].includes(Number(opts.enabledMark))) { errors.push({ field: 'enabledMark', message: 'enabledMark 必须是 0 或 1' }) } return { ok: errors.length === 0, step: 0, stepName: '基础信息', errors, } } export function validateDatasourceCreate(opts: DatasourceOpts): { ok: boolean steps: StepValidation[] } { const s0 = validateBasicInfo(opts) return { ok: s0.ok, steps: [s0] } }