Files
dacli/src/commands/datasource/_validate.ts
T
ssw f3bd0cc088 feat(p3-commands): v0.11e+ Phase D1 org/msg/datasource step machine
新建 3 _validate.ts (P3 命令 step machine 扩展):
- org/_validate.ts (1 step, 镜像 web organize/Form.vue + 后端 OrganizeController + PS036 层级限制)
- msg/_validate.ts (1 step, 镜像 web message/notice/Form.vue + 后端 MessageController + MSERR114 系统编码规则 + 4 种消息类型)
- datasource/_validate.ts (1 step, 镜像 web dataSource/Form.vue + 后端 DbLinkController + DB001 数据库类型 + host/port/user/password 必填)

每个 _validate.ts 完整导出:
- ValidationError + StepValidation interface
- validateBasicInfo
- validate<Datasource>Create

镜像 web 端:
- organize/Form.vue PS013 (parentId 必填, 不能与父级相同) / PS036 (层级限制)
- message/notice/Form.vue MSERR113/114 (模板标题参数 + 系统编码规则)
- dataSource/Form.vue DB001 (6 种数据库类型枚举)

未集成到 create.ts (P3 命令大, 留待后续 session 集成)
2026-06-27 03:24:51 +08:00

101 lines
2.8 KiB
TypeScript

/**
* 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] }
}