v0.11d+ Phase 2.2 闭环 (镜像 + web/backend 行号标注):
**新增 _validate_dm.ts (3 step, 镜像 web dataModel/Form.vue:42-242 + 后端 DbTableController.java:92-112)**:
- step 0 基础信息: 镜像 web dataModel/Form.vue:42-61 schemas
- table 必填 + maxlength 50
- table 正则 (镜像 L50 /(^_([A-Z0-9]_?)*$)|(^[A-Z](_?[A-Z0-9])*_?$)/i)
- tableName 必填 + maxlength 50
- step 1 字段定义: 镜像 web dataModel/Form.vue:174-209 exist() + L213/L215-216 + L19-26 options
- 字段数 ≥ 1 (镜像 L213)
- 至少 1 个 primaryKey (镜像 L215-216)
- 字段 vmodel 正则 (镜像 L184, 含 /i flag 允许大写)
- dataType 白名单 {varchar, int, datetime, decimal, bigint, text} (镜像 L19-26)
- decimal dataLength 格式 ^\d+(?:,\d*)?$ (镜像 L185, 197)
- 字段不重复 (镜像 L191-196)
- fieldName 必填 (镜像 L202-206)
- step 2 关键字+唯一性: 镜像后端 DbTableController.java:92-112 checkName
- Java 关键字 (后端 GenerateConstant.getJavaKeyword 子集)
- SQL 关键字 (后端 GenerateConstant.getSqlKeyword 子集)
- 表名重复 (后端 EXIST001 预校验)
**create.ts 集成**:
- 新增 runDmStepMachine() 在发 API 前聚合校验, 失败 → 1 次报所有错 (AI 不用试错)
- --help + installAiHelpHook 同步标注 web dataModel/Form.vue + 后端 DbTableController 行号
**端到端验证 (tdd10, 9/9 预期匹配)**:
1. 成功路径 (table 合法 + 1 字段 + primaryKey) → 3/3 ✓
2. SQL 关键字 'select' → step 2 拦截
3. 字段数=0 → step 1 拦截 (请至少添加一个字段)
4. 无 primaryKey → step 1 拦截 (请选择一个字段作为主键)
5. 非法 dataType 'xml' → step 1 拦截 (白名单 6 种)
6. F_NAME 字段 → 3/3 ✓ (镜像 web /i flag 行为)
7. 字段重复 → step 1 拦截 (第2行列名已重复)
8. decimal dataLength='abc' → step 1 拦截
9. 表名重复 → step 2 拦截 (EXIST001 预校验)
**关键新发现** (drift 防御):
1. web dm designer 位置: dataModel/Form.vue (不是 sysForm/Form.vue, 那是 form回传)
2. '请在数据库中添加对应的数据表' 实际是 getDbTableModel 抛 (export 端点), 不是 create 误报
3. 字段正则 /i flag: web 允许大写开头字段, dacli 必须镜像
4. 预存 webapi shim bug: loadDataModel.createDataModel 返回 undefined, 范围外 (待后续专项)
**MEMORY**: 新建 dacli-dm-create-step-machine-2026-06-26.md 记录设计意图 + 行号 + 反模式
Co-Authored-By: Claude <[email protected]>
316 lines
11 KiB
TypeScript
316 lines
11 KiB
TypeScript
/**
|
|
* commands/dm/_validate.ts — dm create step machine 校验
|
|
*
|
|
* 镜像 web dataModel/Form.vue handleSubmit (L210-242) + 后端 DbTableController checkName (L92-112) 的前端校验
|
|
* 让 dacli dm create 在发 API 之前暴露问题, AI 不用去浏览器试错
|
|
*
|
|
* 设计原则 (2026-06-26 v0.11d+):
|
|
* - 不是复刻 web UI 行为 (drag/drop/click), 而是 mirror JNPF 业务规则
|
|
* - 跟 form/_validate.ts + menu/_validate.ts 模式一致: 在发 API 前校验, 失败 → 聚合错误, 不发 API
|
|
* - 校验通过 → 1 次 create API call (不拆步骤)
|
|
*
|
|
* step 镜像对应:
|
|
* step 0: 基础信息 (镜像 web dataModel/Form.vue:42-61 schemas)
|
|
* - newTable 必填 + maxlength 50
|
|
* - newTable 正则 (镜像 Form.vue:50) — 允许 _xxx 开头 + 大写字母
|
|
* - tableName 必填 + maxlength 50
|
|
* step 1: 字段定义 (镜像 web dataModel/Form.vue:174-209 exist() + L213/L215-216)
|
|
* - 字段数 ≥ 1
|
|
* - 至少 1 个 primaryKey
|
|
* - 字段 vmodel 正则 (镜像 Form.vue:184)
|
|
* - dataType 白名单 {varchar, int, datetime, decimal, bigint, text} (镜像 Form.vue:19-26)
|
|
* - decimal dataLength 格式 ^\d+(?:,\d*)?$ (镜像 Form.vue:197)
|
|
* - 字段不重复 (镜像 Form.vue:191-196)
|
|
* - fieldName 必填 (镜像 Form.vue:202-206)
|
|
* step 2: 关键字 + 唯一性预校验 (镜像后端 DbTableController.java:92-112 checkName)
|
|
* - Java 关键字 (镜像后端 GenerateConstant.getJavaKeyword)
|
|
* - SQL 关键字 (镜像后端 GenerateConstant.getSqlKeyword)
|
|
* - 表名不重复 (后端 EXIST001 预校验)
|
|
*/
|
|
|
|
export interface ValidationError {
|
|
/** 错误字段 (formId/字段名) */
|
|
field?: string
|
|
/** 错误消息 (镜像 web 文案 + 后端 EXIST 码, AI 能对照) */
|
|
message: string
|
|
}
|
|
|
|
export interface StepValidation {
|
|
ok: boolean
|
|
step: 0 | 1 | 2
|
|
/** step 名称 (用于日志) */
|
|
stepName: string
|
|
errors: ValidationError[]
|
|
}
|
|
|
|
/** 字段对象简化版 (供校验用) */
|
|
export interface DmFieldLite {
|
|
field?: string
|
|
fieldName?: string
|
|
dataType?: string
|
|
dataLength?: string | number
|
|
primaryKey?: number | boolean
|
|
allowNull?: number | boolean
|
|
identity?: number | boolean
|
|
[k: string]: unknown
|
|
}
|
|
|
|
/**
|
|
* newTable 正则 — 镜像 web dataModel/Form.vue:50
|
|
* pattern: /(^_([A-Z0-9]_?)*$)|(^[A-Z](_?[A-Z0-9])*_?$)/i
|
|
* 允许: 大写字母/数字/下划线, 大写字母开头 OR _xxx 开头
|
|
* 不允许: 小写字母开头 / 特殊字符
|
|
* 注意: 与 form 字段 vmodel 正则 (允许小写) 略有不同!
|
|
* msg: '请输入正确的表名称'
|
|
*/
|
|
const NEWTABLE_REGEXP = /(^_([A-Z0-9]_?)*$)|(^[A-Z](_?[A-Z0-9])*_?$)/i
|
|
|
|
/** 字段 vmodel 正则 — 镜像 web dataModel/Form.vue:184
|
|
* reg: /(^_([a-z0-9]_?)*$)|(^[a-z](_?[a-z0-9])*_?$)/i
|
|
* 允许: 字母/数字/下划线, 小写字母开头 OR _xxx 开头
|
|
* msg: '列名格式错误, 请重新输入' */
|
|
const FIELD_REGEXP = /(^_([a-z0-9]_?)*$)|(^[a-z](_?[a-z0-9])*_?$)/i
|
|
|
|
/** decimal dataLength 正则 — 镜像 web dataModel/Form.vue:185, 197
|
|
* 格式: ^\d+(?:,\d*)?$ (例: 12 或 12,4)
|
|
* msg: '请输入数字' (decimal 类型) */
|
|
const DECIMAL_LENGTH_REGEXP = /^\d+(?:,\d*)?$/
|
|
|
|
/** dataType 白名单 — 镜像 web dataModel/Form.vue:19-26 options */
|
|
const VALID_DATA_TYPES = ['varchar', 'int', 'datetime', 'decimal', 'bigint', 'text']
|
|
|
|
/** Java 关键字子集 — 镜像后端 GenerateConstant.getJavaKeyword
|
|
* 完整列表见 jnpf/.../util/GenerateConstant.getJavaKeyword() */
|
|
const JAVA_KEYWORDS = new Set([
|
|
'abstract', 'assert', 'boolean', 'break', 'byte', 'case', 'catch', 'char', 'class',
|
|
'const', 'continue', 'default', 'do', 'double', 'else', 'enum', 'extends', 'final',
|
|
'finally', 'float', 'for', 'goto', 'if', 'implements', 'import', 'instanceof', 'int',
|
|
'interface', 'long', 'native', 'new', 'package', 'private', 'protected', 'public',
|
|
'return', 'short', 'static', 'strictfp', 'super', 'switch', 'synchronized', 'this',
|
|
'throw', 'throws', 'transient', 'try', 'void', 'volatile', 'while',
|
|
])
|
|
|
|
/** SQL 关键字子集 — 镜像后端 GenerateConstant.getSqlKeyword */
|
|
const SQL_KEYWORDS = new Set([
|
|
'select', 'insert', 'update', 'delete', 'from', 'where', 'and', 'or', 'not', 'null',
|
|
'create', 'drop', 'alter', 'table', 'index', 'view', 'database', 'schema',
|
|
'primary', 'foreign', 'key', 'unique', 'check', 'default', 'constraint',
|
|
'join', 'inner', 'left', 'right', 'outer', 'on', 'as', 'group', 'by', 'having',
|
|
'order', 'limit', 'offset', 'union', 'distinct', 'all', 'any', 'in', 'between',
|
|
'like', 'is', 'exists', 'case', 'when', 'then', 'else', 'end',
|
|
'count', 'sum', 'avg', 'min', 'max',
|
|
'int', 'integer', 'varchar', 'char', 'text', 'date', 'datetime', 'timestamp', 'decimal',
|
|
'numeric', 'float', 'double', 'boolean', 'bigint', 'smallint', 'tinyint',
|
|
])
|
|
|
|
/**
|
|
* step 0: 基础信息校验
|
|
*
|
|
* 镜像 web dataModel/Form.vue:42-61 schemas (newTable + tableName)
|
|
*/
|
|
export function validateBasicInfo(opts: {
|
|
table: string
|
|
tableName: string
|
|
}): StepValidation {
|
|
const errors: ValidationError[] = []
|
|
|
|
// 镜像 Form.vue:43-52 schemas[newTable] rules
|
|
if (!opts.table || !opts.table.trim()) {
|
|
errors.push({ field: 'table', message: '表名称不能为空' })
|
|
} else if (opts.table.length > 50) {
|
|
errors.push({ field: 'table', message: '表名称不能超过 50 个字符' })
|
|
} else if (!NEWTABLE_REGEXP.test(opts.table)) {
|
|
errors.push({ field: 'table', message: '请输入正确的表名称' })
|
|
}
|
|
|
|
// 镜像 Form.vue:54-58 schemas[tableName]
|
|
if (!opts.tableName || !opts.tableName.trim()) {
|
|
errors.push({ field: 'tableName', message: '表说明不能为空' })
|
|
} else if (opts.tableName.length > 50) {
|
|
errors.push({ field: 'tableName', message: '表说明不能超过 50 个字符' })
|
|
}
|
|
|
|
return {
|
|
ok: errors.length === 0,
|
|
step: 0,
|
|
stepName: '基础信息',
|
|
errors,
|
|
}
|
|
}
|
|
|
|
/**
|
|
* step 1: 字段定义校验
|
|
*
|
|
* 镜像 web dataModel/Form.vue:174-209 exist() + L213 + L215-216
|
|
*/
|
|
export function validateFields(opts: {
|
|
fields: DmFieldLite[]
|
|
}): StepValidation {
|
|
const errors: ValidationError[] = []
|
|
|
|
// 镜像 Form.vue:213 if (!fieldList.value.length)
|
|
if (!opts.fields || opts.fields.length === 0) {
|
|
errors.push({ field: 'fields', message: '请至少添加一个字段' })
|
|
return { ok: false, step: 1, stepName: '字段定义', errors }
|
|
}
|
|
|
|
// 镜像 Form.vue:215-216 至少 1 个 primaryKey
|
|
const hasPrimaryKey = opts.fields.some((f) => !!f.primaryKey)
|
|
if (!hasPrimaryKey) {
|
|
errors.push({
|
|
field: 'fields.primaryKey',
|
|
message: '请选择一个字段作为主键',
|
|
})
|
|
}
|
|
|
|
// 镜像 Form.vue:174-209 exist() 字段级 7 项校验
|
|
const seenFields = new Set<string>()
|
|
opts.fields.forEach((f, i) => {
|
|
const row = i + 1
|
|
// 镜像 L178-181 field 必填
|
|
if (!f.field || !String(f.field).trim()) {
|
|
errors.push({
|
|
field: `fields[${i}].field`,
|
|
message: `第${row}行列名不能为空`,
|
|
})
|
|
} else if (!FIELD_REGEXP.test(String(f.field))) {
|
|
// 镜像 L184-190 field 正则
|
|
errors.push({
|
|
field: `fields[${i}].field`,
|
|
message: `第${row}行列名格式错误, 请重新输入`,
|
|
})
|
|
} else if (seenFields.has(String(f.field))) {
|
|
// 镜像 L191-196 field 重复
|
|
errors.push({
|
|
field: `fields[${i}].field`,
|
|
message: `第${row}行列名'${f.field}'已重复`,
|
|
})
|
|
} else {
|
|
seenFields.add(String(f.field))
|
|
}
|
|
|
|
// 镜像 L197-201 decimal dataLength 格式
|
|
if (f.dataType === 'decimal' && f.dataLength !== undefined) {
|
|
if (!DECIMAL_LENGTH_REGEXP.test(String(f.dataLength))) {
|
|
errors.push({
|
|
field: `fields[${i}].dataLength`,
|
|
message: `第${row}行请输入数字 (decimal 格式: 12 或 12,4)`,
|
|
})
|
|
}
|
|
}
|
|
|
|
// 镜像 L202-206 fieldName 必填
|
|
if (!f.fieldName || !String(f.fieldName).trim()) {
|
|
errors.push({
|
|
field: `fields[${i}].fieldName`,
|
|
message: `第${row}行说明不能为空`,
|
|
})
|
|
}
|
|
|
|
// 镜像 L19-26 options dataType 白名单 (新增, web 端用下拉框, 限制更严)
|
|
if (f.dataType && !VALID_DATA_TYPES.includes(String(f.dataType))) {
|
|
errors.push({
|
|
field: `fields[${i}].dataType`,
|
|
message: `第${row}行数据类型必须为 ${VALID_DATA_TYPES.join('/')} 之一, 当前 ${f.dataType}`,
|
|
})
|
|
}
|
|
})
|
|
|
|
return {
|
|
ok: errors.length === 0,
|
|
step: 1,
|
|
stepName: '字段定义',
|
|
errors,
|
|
}
|
|
}
|
|
|
|
/**
|
|
* step 2: 关键字 + 唯一性预校验
|
|
*
|
|
* 镜像后端 DbTableController.java:92-112 checkName
|
|
* - checkName: 表名 + 字段名不能是 Java/SQL 关键字
|
|
* - 后端 createTable status 0 → EXIST001 (重复)
|
|
*/
|
|
export function validateKeywordAndUniqueness(opts: {
|
|
table: string
|
|
fields: DmFieldLite[]
|
|
/** 当前 dbLinkId 下已存在的表名 (供唯一性预校验) */
|
|
existingTables?: string[]
|
|
}): StepValidation {
|
|
const errors: ValidationError[] = []
|
|
|
|
const tableLower = (opts.table || '').toLowerCase()
|
|
// 镜像后端 checkName L96-98 表名关键字
|
|
if (JAVA_KEYWORDS.has(tableLower) || SQL_KEYWORDS.has(tableLower)) {
|
|
errors.push({
|
|
field: 'table',
|
|
message: `表名称"${opts.table}"不能是 Java/SQL 关键字 (后端 checkName 镜像)`,
|
|
})
|
|
}
|
|
|
|
// 镜像后端 checkName L99-110 字段名关键字
|
|
if (opts.fields && opts.fields.length > 0) {
|
|
const conflictFields: string[] = []
|
|
opts.fields.forEach((f) => {
|
|
if (!f.field) return
|
|
const fieldLower = String(f.field).toLowerCase()
|
|
if (JAVA_KEYWORDS.has(fieldLower) || SQL_KEYWORDS.has(fieldLower)) {
|
|
conflictFields.push(String(f.field))
|
|
}
|
|
})
|
|
if (conflictFields.length > 0) {
|
|
errors.push({
|
|
field: 'fields',
|
|
message: `列名${conflictFields.join(',')}不能是 Java/SQL 关键字 (后端 checkName 镜像)`,
|
|
})
|
|
}
|
|
}
|
|
|
|
// 镜像后端 EXIST001 表名重复 (后端会再查, 这是预校验)
|
|
if (opts.existingTables && opts.existingTables.length > 0) {
|
|
if (opts.existingTables.includes(opts.table)) {
|
|
errors.push({
|
|
field: 'table',
|
|
message: `表名称"${opts.table}"已存在 (后端 EXIST001 预校验)`,
|
|
})
|
|
}
|
|
}
|
|
|
|
return {
|
|
ok: errors.length === 0,
|
|
step: 2,
|
|
stepName: '关键字与唯一性',
|
|
errors,
|
|
}
|
|
}
|
|
|
|
/** 跑全部 step 校验, 返回聚合结果 */
|
|
export function validateDmCreate(opts: {
|
|
table: string
|
|
tableName: string
|
|
fields: DmFieldLite[]
|
|
existingTables?: string[]
|
|
}): {
|
|
ok: boolean
|
|
steps: StepValidation[]
|
|
} {
|
|
const s0 = validateBasicInfo({
|
|
table: opts.table,
|
|
tableName: opts.tableName,
|
|
})
|
|
|
|
const s1 = validateFields({
|
|
fields: opts.fields,
|
|
})
|
|
|
|
const s2 = validateKeywordAndUniqueness({
|
|
table: opts.table,
|
|
fields: opts.fields,
|
|
existingTables: opts.existingTables,
|
|
})
|
|
|
|
return {
|
|
ok: s0.ok && s1.ok && s2.ok,
|
|
steps: [s0, s1, s2],
|
|
}
|
|
} |