新建 src/commands/_error_codes.ts (335 行):
- ErrorCode union type (top 30 常用: EXIST001/002/101/102/103/104, FA001/002/003/019/021/028/029, VS024/401/402/403/408/409/411/412, SYS014/017, DB201/302, WF025/054, FM005/007, OTHER 兜底)
- ErrorCategory enum (duplicate/not_found/permission/invalid_state/db/workflow/visual_dev/unknown)
- ALL_ERROR_CODES 字典 (code → { code, category, rawMessage, suggestion })
- classify(msg) — regex 提取 EXIST/FA/VS/SYS/DB/WF/FM prefix
- format(code, ctx) — 替换 suggestion 占位符
- aggregate(errors) — 按 code + category 分桶
- formatErrorLine(err, { step, stepName }) — 输出 '[step N name] field: CODE - message'
- enhanceFromBackend(msg, field, ctx) — catch 后端错一键增强
集成 7 _validate.ts (form/menu/dm/workflow/dict/role/di):
- ValidationError 加 code?: string 字段
- 关键错误标 code: EXIST001/002/104, VS030/401/402/408, WF054 等
集成 7 create/update.ts:
- catch 后端 r.msg 时调 enhanceFromBackend() 自动填 code
- 输出格式: '[step N name] field: CODE - message (suggestion)'
SKILL.md 加 v0.11e+ Phase A1 段 (架构/权威源/web 镜像/集成/验证)
端到端 tdd10 验证 (admin/123456, appCode=tdd10):
- EXIST002: dict typeCreate enCode 重复 → [step 1 唯一性] enCode: EXIST002 - 字典编码已存在
- EXIST001: dict typeCreate fullName 重复 → [step 1 唯一性] fullName: EXIST001 - 字典名称已存在
selfcheck 8/8 pass (_error_codes.ts + 7 _validate.ts)
镜像 web Form.vue:419 validatePopoverRef 字段级 + 后端 MsgCode.java:50-783 错误码体系
374 lines
13 KiB
TypeScript
374 lines
13 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
|
|
/** v0.11e+: ErrorCode (镜像后端 MsgCode.java, AI 能查表)
|
|
* 复刻 _error_codes.ts 的 ValidationError 字段, 让 step machine 输出格式统一 */
|
|
code?: 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']
|
|
|
|
/** 需要 dataLength 的 dataType — 镜像 web dataModel/Form.vue:278
|
|
* `:disabled="record.dataType !== 'varchar' && record.dataType !== 'decimal'"`
|
|
* 只有 varchar 和 decimal 必填 length, 其他 (int/bigint/datetime/text) 不需要 */
|
|
const TYPES_REQUIRING_LENGTH = new Set(['varchar', 'decimal'])
|
|
|
|
/** 系统自动注入的 11 sysCols — 镜像 dacli create.ts L107-119
|
|
* 用户字段如果含这些会冲突 (BS-38: f_description; 同样问题适用于其他 10 sysCols)
|
|
* ⚠️ web 端不显示 sysCols, 所以 web 不需要校验; dacli 必须强制拦截 */
|
|
const SYSTEM_COLUMNS = new Set([
|
|
'f_id',
|
|
'f_tenant_id',
|
|
'f_flow_task_id',
|
|
'f_flow_id',
|
|
'f_flow_state',
|
|
'f_sort_code',
|
|
'f_create_user',
|
|
'f_create_time',
|
|
'f_modify_user',
|
|
'f_modify_time',
|
|
'f_description',
|
|
])
|
|
|
|
/** 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 if (SYSTEM_COLUMNS.has(String(f.field).toLowerCase())) {
|
|
// Defect 1: 11 系统列冲突 (BS-38 普遍化, 不止 f_description)
|
|
// web 端用户看不到 sysCols, 但 dacli 会注入 → 必须拦截
|
|
errors.push({
|
|
field: `fields[${i}].field`,
|
|
message: `第${row}行列名'${f.field}'与系统自动注入列冲突 (${Array.from(SYSTEM_COLUMNS).join('/')} 是 dacli 自动加的)`,
|
|
})
|
|
} else {
|
|
seenFields.add(String(f.field))
|
|
}
|
|
|
|
// Defect 2: dataType 必填 + 白名单
|
|
// 镜像 Form.vue:283 jnpf-select placeholder="请选择" (用户选空就报错)
|
|
const dataType = f.dataType ? String(f.dataType).trim() : ''
|
|
if (!dataType) {
|
|
errors.push({
|
|
field: `fields[${i}].dataType`,
|
|
message: `第${row}行数据类型不能为空 (可选: ${VALID_DATA_TYPES.join('/')})`,
|
|
})
|
|
} else if (!VALID_DATA_TYPES.includes(dataType)) {
|
|
errors.push({
|
|
field: `fields[${i}].dataType`,
|
|
message: `第${row}行数据类型必须为 ${VALID_DATA_TYPES.join('/')} 之一, 当前 ${dataType}`,
|
|
})
|
|
}
|
|
|
|
// Defect 3: dataType 与 dataLength 关系
|
|
// 镜像 Form.vue:278 :disabled="record.dataType !== 'varchar' && record.dataType !== 'decimal'"
|
|
// - varchar/decimal: 必填 length
|
|
// - int/bigint/datetime/text: 不需要 length (web 端 input 禁用)
|
|
if (TYPES_REQUIRING_LENGTH.has(dataType)) {
|
|
if (f.dataLength === undefined || f.dataLength === null || String(f.dataLength).trim() === '') {
|
|
errors.push({
|
|
field: `fields[${i}].dataLength`,
|
|
message: `第${row}行 dataType=${dataType} 必须填 dataLength (varchar 数字, decimal 格式 12 或 12,4)`,
|
|
})
|
|
} else if (dataType === 'decimal' && !DECIMAL_LENGTH_REGEXP.test(String(f.dataLength))) {
|
|
// 镜像 L197-201 decimal dataLength 格式
|
|
errors.push({
|
|
field: `fields[${i}].dataLength`,
|
|
message: `第${row}行请输入数字 (decimal 格式: 12 或 12,4)`,
|
|
})
|
|
}
|
|
}
|
|
|
|
// Defect 4: primaryKey 字段 allowNull 必须 false
|
|
// 镜像 Form.vue:292-293 <a-checkbox :disabled="!!record.primaryKey"> (primaryKey 强制非空)
|
|
if (f.primaryKey && f.allowNull) {
|
|
errors.push({
|
|
field: `fields[${i}].allowNull`,
|
|
message: `第${row}行主键字段不允许为空 (allowNull 必须为 0/false)`,
|
|
})
|
|
}
|
|
|
|
// 镜像 L202-206 fieldName 必填
|
|
if (!f.fieldName || !String(f.fieldName).trim()) {
|
|
errors.push({
|
|
field: `fields[${i}].fieldName`,
|
|
message: `第${row}行说明不能为空`,
|
|
})
|
|
}
|
|
})
|
|
|
|
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 预校验)`,
|
|
code: '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],
|
|
}
|
|
} |