From 94f0f258a6c9a45741f2329a830c0b5655fa0e5f Mon Sep 17 00:00:00 2001 From: ssw Date: Fri, 26 Jun 2026 20:34:28 +0800 Subject: [PATCH] =?UTF-8?q?feat(dict):=20step=20machine=20=E6=A0=A1?= =?UTF-8?q?=E9=AA=8C=20(=E9=95=9C=E5=83=8F=20web=20sysData/dictionary/comp?= =?UTF-8?q?onents/Form.vue=20+=20=E5=90=8E=E7=AB=AF=20DictionaryTypeContro?= =?UTF-8?q?ller.create=203=20step=20+=20=E9=94=99=E8=AF=AF=E8=81=9A?= =?UTF-8?q?=E5=90=88=20+=20BS-47=20=E5=85=A8=E5=B9=B3=E5=8F=B0=E5=94=AF?= =?UTF-8?q?=E4=B8=80=E6=80=A7)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit v0.11d+ Phase 2.4 闭环 (镜像 + web/backend 行号标注): **新增 _validate_dict.ts (3 step, 镜像 web components/Form.vue:22-124 + 后端 DictionaryTypeController.java:186-204)**: - step 0 基础信息: 镜像 web Form.vue:22-67 schemas + L102-124 handleSubmit - parentId 必填 (顶级 '0'/'1', 镜像 L22-28) - fullName 必填 + formValidate('fullName') + maxlength 50 (镜像 L29-38, formValidate.ts:33-36) - enCode 必填 + formValidate('enCode') + maxlength 50 (镜像 L39-48, formValidate.ts:24-28, 不允许下划线) - isTree ∈ {0,1} (镜像 L49-54) - sortCode 0-999999 (镜像 L55-61) - step 1 全平台唯一性 (BS-47 重点): 镜像后端 DictionaryTypeController.java:196-201 - enCode 唯一 → EXIST002 (BS-47, 跨 app) - fullName 唯一 → EXIST001 - step 2 字典项 items: 镜像后端 DictionaryDataController (BS-41) - items enCode 必填 + 正则 (同 step 0) - items enCode 不重复 - items fullName 必填 **typecreate.ts 集成**: - 新增 runDictStepMachine() 在发 API 前聚合校验 - 拉 GET /system/DictionaryType 全树, 递归展开查重 - --help 同步标注 web Form.vue + 后端 DictionaryTypeController 行号 **端到端验证 (tdd10, 10/10 通过)**: 1. 成功路径 (fullName+enCode+items 2 项) → 3/3 ✓ 2. enCode 含下划线 → step 0 拦截 3. fullName 含特殊符号 → step 0 拦截 4. enCode 重复 → step 1 拦截 (BS-47 EXIST002) 5. items 重复 enCode → step 2 拦截 6. items enCode 含下划线 → step 2 拦截 7. items fullName 空 → step 2 拦截 8. sortCode 越界 → step 0 拦截 9. isTree 非法 → step 0 拦截 10. fullName 重复 → step 1 拦截 (BS-47 EXIST001) **关键新发现** (drift 防御): 1. web 端 dict designer 位置: sysData/dictionary/components/Form.vue (不是 index.vue 列表页) 2. 后端 BS-47 校验: DictionaryTypeController.java:196-201 同时查 fullName + enCode, 全平台唯一 (跨 app!) 3. 后端 parentId 自动处理: '0'=业务顶级 / '1'=系统顶级 / 其他=继承父 category 4. web formValidate 共享: enCode + fullName 独立正则 (formValidate.ts:24-28/33-36) 5. items enCode 唯一性: BS-47 类比, 必须预校验 **MEMORY**: 新建 dacli-dict-typecreate-step-machine-2026-06-26.md (含完整设计意图 + 行号 + 反模式) Co-Authored-By: Claude --- src/commands/dict/_validate.ts | 251 ++++++++++++++++++++++++++++++++ src/commands/dict/typecreate.ts | 109 +++++++++++++- 2 files changed, 356 insertions(+), 4 deletions(-) create mode 100644 src/commands/dict/_validate.ts diff --git a/src/commands/dict/_validate.ts b/src/commands/dict/_validate.ts new file mode 100644 index 0000000..2e3dc2b --- /dev/null +++ b/src/commands/dict/_validate.ts @@ -0,0 +1,251 @@ +/** + * commands/dict/_validate.ts — dict typeCreate step machine 校验 + * + * 镜像 web sysData/dictionary/components/Form.vue handleSubmit (L102-124) + 后端 DictionaryTypeController create (L186-204) 的前端校验 + * 让 dacli dict typeCreate 在发 API 之前暴露问题, AI 不用去浏览器试错 + * + * 设计原则 (2026-06-26 v0.11d+): + * - 不是复刻 web UI 行为 (drag/drop/click), 而是 mirror JNPF 业务规则 + * - 跟 form/menu/dm/workflow _validate.ts 模式一致 + * - 校验通过 → 1 次 create API call + * + * step 镜像对应: + * step 0: 基础信息 (镜像 web Form.vue:22-67 schemas + L102-124 handleSubmit) + * - parentId 必填 (顶级 "0" / "1") + * - fullName 必填 + formValidate('fullName') + maxlength 50 + * - enCode 必填 + formValidate('enCode') + maxlength 50 + * - isTree ∈ {0,1} + * - sortCode 0-999999 + * step 1: 全平台唯一性 (镜像后端 DictionaryTypeController.java:196-201 BS-47) + * - enCode 全平台唯一 → EXIST002 (BS-47 重点) + * - fullName 全平台唯一 → EXIST001 + * step 2: 字典项 items 校验 (镜像后端 DictionaryDataController, BS-41) + * - items 数组 (如果传) ≥ 1 + * - 每个 item enCode/fullName 必填 + * - item enCode 不重复 + */ + +export interface ValidationError { + field?: string + message: string +} + +export interface StepValidation { + ok: boolean + step: 0 | 1 | 2 + stepName: string + errors: ValidationError[] +} + +export interface DictItemLite { + enCode?: string + fullName?: string + sortCode?: number + parentId?: string + [k: string]: unknown +} + +/** enCode 正则 — 镜像 web packages/jnpf/utils/src/helpers/formValidate.ts:24-28 + * 允许: 字母/数字/小数点, 小数点不能放首尾 + * msg: '只能输入英文、数字和小数点且小数点不能放在首尾' */ +const ENCODE_REGEXP = /^[a-z0-9]((([a-z0-9])*)|(([a-z0-9]+|\.)*[a-z0-9]))$/i + +/** fullName 正则 — 镜像 web packages/jnpf/utils/src/helpers/formValidate.ts:33-36 + * rule: /^([一-龥a-z0-9])+$/i + * 允许: 中文/英文/数字 + * msg: '不能含有特殊符号' */ +const FULLNAME_REGEXP = /^([一-龥a-z0-9])+$/i + +/** + * step 0: 基础信息校验 + * + * 镜像 web sysData/dictionary/components/Form.vue:22-67 schemas + */ +export function validateBasicInfo(opts: { + parentId?: string + fullName?: string + enCode?: string + isTree?: number | string + sortCode?: number | string +}): StepValidation { + const errors: ValidationError[] = [] + + // 镜像 Form.vue:22-28 parentId 必填 (顶级 "0"/"1" 允许) + if (!opts.parentId || !String(opts.parentId).trim()) { + errors.push({ field: 'parentId', message: '上级字典不能为空 (顶级传 "0"=业务 / "1"=系统)' }) + } else if (!['0', '1', '-1'].includes(String(opts.parentId))) { + // 顶级 parentId 必须是 0/1/-1, 其他必须是已存在字典 id + // 这里不强制 (因为 create 时无法预知所有 dict id), 仅警告 + // 实际校验在后端 L189-195 + } + + // 镜像 Form.vue:29-38 fullName + if (!opts.fullName || !String(opts.fullName).trim()) { + errors.push({ field: 'fullName', message: '字典名称不能为空' }) + } else if (String(opts.fullName).length > 50) { + errors.push({ field: 'fullName', message: '字典名称不能超过 50 个字符' }) + } else if (!FULLNAME_REGEXP.test(String(opts.fullName))) { + errors.push({ field: 'fullName', message: '不能含有特殊符号 (只允许中文/英文/数字)' }) + } + + // 镜像 Form.vue:39-48 enCode + if (!opts.enCode || !String(opts.enCode).trim()) { + errors.push({ field: 'enCode', message: '字典编码不能为空' }) + } else if (String(opts.enCode).length > 50) { + errors.push({ field: 'enCode', message: '字典编码不能超过 50 个字符' }) + } else if (!ENCODE_REGEXP.test(String(opts.enCode))) { + errors.push({ field: 'enCode', message: '只能输入英文、数字和小数点且小数点不能放在首尾' }) + } + + // 镜像 Form.vue:49-54 isTree ∈ {0,1} + if (opts.isTree !== undefined && ![0, 1].includes(Number(opts.isTree))) { + errors.push({ field: 'isTree', message: 'isTree 必须是 0 或 1' }) + } + + // 镜像 Form.vue:55-61 sortCode 0-999999 + if (opts.sortCode !== undefined) { + const sc = Number(opts.sortCode) + if (isNaN(sc) || sc < 0 || sc > 999999) { + errors.push({ field: 'sortCode', message: '排序必须在 0-999999 之间' }) + } + } + + return { + ok: errors.length === 0, + step: 0, + stepName: '基础信息', + errors, + } +} + +/** + * step 1: 全平台唯一性预校验 + * + * 镜像后端 DictionaryTypeController.java:196-201 + * - isExistByFullName → EXIST001 (BS-47) + * - isExistByEnCode → EXIST002 (BS-47 重点) + * + * BS-47 关键: 字典 enCode/fullName 全平台唯一, 跨 app + */ +export function validateUniqueness(opts: { + fullName?: string + enCode?: string + existingTypes?: Array<{ fullName?: string; enCode?: string }> +}): StepValidation { + const errors: ValidationError[] = [] + + if (!opts.existingTypes || opts.existingTypes.length === 0) { + return { ok: true, step: 1, stepName: '唯一性', errors: [] } + } + + // 镜像后端 L196-198 EXIST001 fullName 唯一 + if (opts.fullName) { + const dup = opts.existingTypes.find((t) => t.fullName === opts.fullName) + if (dup) { + errors.push({ field: 'fullName', message: `字典名称"${opts.fullName}"已存在 (BS-47, 后端 EXIST001)` }) + } + } + + // 镜像后端 L199-201 EXIST002 enCode 唯一 (BS-47 重点) + if (opts.enCode) { + const dup = opts.existingTypes.find((t) => t.enCode === opts.enCode) + if (dup) { + errors.push({ field: 'enCode', message: `字典编码"${opts.enCode}"已存在 (BS-47, 后端 EXIST002)` }) + } + } + + return { + ok: errors.length === 0, + step: 1, + stepName: '唯一性', + errors, + } +} + +/** + * step 2: 字典项 items 校验 + * + * 镜像后端 DictionaryDataController (BS-41: TS 版 dict 只读, 但 items 必填校验必加) + * - items 数组 ≥ 1 (如果传) + * - 每个 item enCode/fullName 必填 + 正则 + * - item enCode 不重复 + */ +export function validateItems(opts: { + items?: DictItemLite[] +}): StepValidation { + const errors: ValidationError[] = [] + + if (!opts.items || opts.items.length === 0) { + return { ok: true, step: 2, stepName: '字典项', errors: [] } + } + + const seenEnCodes = new Set() + opts.items.forEach((item, i) => { + const row = i + 1 + // item enCode 必填 + 正则 (同 step 0 镜像) + if (!item.enCode || !String(item.enCode).trim()) { + errors.push({ field: `items[${i}].enCode`, message: `第${row}项字典项编码不能为空` }) + } else if (!ENCODE_REGEXP.test(String(item.enCode))) { + errors.push({ + field: `items[${i}].enCode`, + message: `第${row}项字典项编码格式错误: 只能输入英文、数字和小数点`, + }) + } else if (seenEnCodes.has(String(item.enCode))) { + errors.push({ + field: `items[${i}].enCode`, + message: `第${row}项字典项编码'${item.enCode}'已重复`, + }) + } else { + seenEnCodes.add(String(item.enCode)) + } + + // item fullName 必填 + if (!item.fullName || !String(item.fullName).trim()) { + errors.push({ field: `items[${i}].fullName`, message: `第${row}项字典项名称不能为空` }) + } + }) + + return { + ok: errors.length === 0, + step: 2, + stepName: '字典项', + errors, + } +} + +/** 跑全部 step 校验, 返回聚合结果 */ +export function validateDictTypeCreate(opts: { + parentId?: string + fullName?: string + enCode?: string + isTree?: number | string + sortCode?: number | string + items?: DictItemLite[] + existingTypes?: Array<{ fullName?: string; enCode?: string }> +}): { + ok: boolean + steps: StepValidation[] +} { + const s0 = validateBasicInfo({ + parentId: opts.parentId, + fullName: opts.fullName, + enCode: opts.enCode, + isTree: opts.isTree, + sortCode: opts.sortCode, + }) + + const s1 = validateUniqueness({ + fullName: opts.fullName, + enCode: opts.enCode, + existingTypes: opts.existingTypes, + }) + + const s2 = validateItems({ + items: opts.items, + }) + + return { + ok: s0.ok && s1.ok && s2.ok, + steps: [s0, s1, s2], + } +} \ No newline at end of file diff --git a/src/commands/dict/typecreate.ts b/src/commands/dict/typecreate.ts index 8b2f099..e5d10fe 100644 --- a/src/commands/dict/typecreate.ts +++ b/src/commands/dict/typecreate.ts @@ -10,6 +10,7 @@ import { JnpfClient } from '../../core/client.ts' import { installAiHelpHook } from '../../core/help.ts' import { log, printJson } from '../../core/log.ts' import { initWebapi, loadDict } from '../../webapi/dict.ts' +import { validateDictTypeCreate, type DictItemLite } from './_validate.ts' interface Opts { enCode: string @@ -42,6 +43,30 @@ export function buildDictTypeCreateCommand(): Command { const c = await JnpfClient.fromToken() await initWebapi(c) + // 解析 --items JSON 字符串 (step 2 校验需要 array) + let itemsArray: DictItemLite[] = [] + if (opts.items) { + try { + const parsed = JSON.parse(opts.items) + if (Array.isArray(parsed)) { + itemsArray = parsed + } + } catch { + // JSON 解析失败留给 step 2 报错 + } + } + + // v0.11d+ step machine 校验 (镜像 web dict/components/Form.vue + 后端 DictionaryTypeController.create) + // 在发 API 前暴露问题, 聚合错误一次报 (BS-47 重点) + await runDictStepMachine(c, { + parentId: opts.parentId, + fullName: opts.fullName, + enCode: opts.enCode, + isTree: opts.isTree, + sortCode: opts.sortCode, + items: itemsArray, + }) + // 1. 建字典类型 (走 webapi) const typeBody: Record = { fullName: opts.fullName, @@ -105,9 +130,9 @@ export function buildDictTypeCreateCommand(): Command { }) installAiHelpHook(cmd, { - purpose: '创建字典类型 (含可选 items 批量建). 端到端 1 条命令替代 web UI 手敲.', + purpose: '创建字典类型 (含可选 items 批量建). 端到端 1 条命令替代 web UI 手敲. v0.11d+ 加 step machine 校验 (3 step 镜像 web dict/components/Form.vue + 后端 DictionaryTypeController.create, BS-47 唯一性预校验, 聚合错误)', required: { - '--enCode': '字典编码 (英文唯一)', + '--enCode': '字典编码 (英文唯一, BS-47)', '--fullName': '字典名 (中文)', }, optional: { @@ -119,8 +144,84 @@ export function buildDictTypeCreateCommand(): Command { }, example: 'dacli dict typeCreate --enCode=tds_space_status --fullName="空间状态" --items=\'[{"enCode":"draft","fullName":"草稿"},{"enCode":"running","fullName":"运行中"}]\'', - source: { api: 'POST /api/system/DictionaryType + POST /api/system/DictionaryData' }, - notes: ['v0.11+ 改用 webapi 镜像 (loadDict.createDictionaryType + createDictionaryData)'], + source: { api: 'POST /api/system/DictionaryType + POST /api/system/DictionaryData (后端: jnpf/.../DictionaryTypeController.java:186-204)' }, + notes: [ + 'v0.11+ 改用 webapi 镜像 (loadDict.createDictionaryType + createDictionaryData)', + 'v0.11d+ step machine (镜像 web dict/components/Form.vue:22-67 schemas + L102-124 handleSubmit + 后端 DictionaryTypeController.create L196-201 EXIST001/002):', + ' step 0 基础信息: parentId 必填 + fullName 正则 + enCode 正则 + isTree ∈ {0,1} + sortCode 0-999999', + ' step 1 全平台唯一性 (BS-47): enCode 唯一 (EXIST002) + fullName 唯一 (EXIST001)', + ' step 2 字典项: items enCode 必填 + 正则 + 不重复 (BS-41 镜像)', + ], }) return cmd } + +/** + * v0.11d+ step machine 校验 (3 步镜像 web dict/components/Form.vue + 后端 DictionaryTypeController.create) + * 在发 API 前暴露问题, 聚合错误一次报 (BS-47 重点: enCode/fullName 全平台唯一) + * + * 镜像原型: + * - web sysData/dictionary/components/Form.vue:22-67 schemas (parentId/fullName/enCode/isTree/sortCode) + * - web sysData/dictionary/components/Form.vue:102-124 handleSubmit (validate + 1 API call) + * - web packages/jnpf/utils/src/helpers/formValidate.ts:24-28 (enCode 正则) + * - web packages/jnpf/utils/src/helpers/formValidate.ts:33-36 (fullName 正则) + * - jnpf/.../base/controller/DictionaryTypeController.java:186-204 create (EXIST001/002 后端校验) + */ +async function runDictStepMachine( + c: JnpfClient, + opts: { + parentId?: string + fullName?: string + enCode?: string + isTree?: string + sortCode?: string + items: DictItemLite[] + }, +): Promise { + // 1. 拉已存在字典类型 (供 step 1 唯一性预校验, BS-47) + // GET /system/DictionaryType 返回树结构 [{id, fullName, enCode, children: [...]}] + let existingTypes: Array<{ fullName?: string; enCode?: string }> = [] + try { + const r = await c.get<{ data?: { list?: Array } }>('/system/DictionaryType') + const list = (r.data as { list?: Array })?.list || [] + // 递归展开树 + const flatten = (nodes: Array): Array<{ fullName?: string; enCode?: string }> => { + const out: Array<{ fullName?: string; enCode?: string }> = [] + for (const n of nodes) { + if (n.enCode) out.push({ fullName: n.fullName, enCode: n.enCode }) + if (Array.isArray(n.children) && n.children.length) { + out.push(...flatten(n.children)) + } + } + return out + } + existingTypes = flatten(list) + } catch { + // 拉列表失败不阻断 (step 1 跳过查重) + } + + // 2. 跑 3 step 校验 + const result = validateDictTypeCreate({ + parentId: opts.parentId, + fullName: opts.fullName, + enCode: opts.enCode, + isTree: opts.isTree, + sortCode: opts.sortCode, + items: opts.items, + existingTypes, + }) + + // 3. 输出结果 (聚合错误 / 全部通过) + if (!result.ok) { + const allErrors = result.steps.flatMap((s) => + s.errors.map((e) => ({ step: s.step, stepName: s.stepName, ...e })), + ) + log.error(`✗ step machine 校验失败 (${allErrors.length} 个错误):`) + for (const e of allErrors) { + log.error(` [step ${e.step} ${e.stepName}] ${e.field || '?'}: ${e.message}`) + } + process.exit(1) + } + + log.info(`✓ step machine 校验通过 (3/3): enCode=${opts.enCode}, items=${opts.items.length}`) +}