From ebaed7a5167ecf05565e0cb667a0f437294aa403 Mon Sep 17 00:00:00 2001 From: ssw Date: Mon, 22 Jun 2026 21:59:28 +0800 Subject: [PATCH] =?UTF-8?q?v0.11:=20=E6=89=A9=E5=B1=95=20form=20create=20?= =?UTF-8?q?=E6=94=AF=E6=8C=81=20field/fieldName=20=E5=AD=97=E6=AE=B5?= =?UTF-8?q?=E6=A0=BC=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 背景: docs/jxcj/design-jnpf.html 28 个 form create 命令用 {"field":"f_X","fieldName":"Y","jnpfKey":"Z","config":{"required":true}} 格式, 但 dacli _simplify.ts 只接受 vmodel/label/required, throw '缺 vmodel 字段'. dm create 已支持 field/fieldName, form create 不对称. 修复: _simplify.ts 新增 normalizeField(), 把 field/fieldName/config:{required} 自动归一化到 vmodel/label/required, 然后走原 wrap 逻辑. vmodel 格式仍兼容. 单测 4/4 PASS (vmodel格式/field格式/relationForm双格式/缺字段throw) 实测 --fields='[{field,fieldName,...}]' form create 成功, btnsList 含新增/导出/导入/批量删除/批量打印, columnBtnsList 含编辑/删除/详情, BS-52 validate PASS. 设计 HTML 56 个 dm/form 命令现在全部可 dispatch 不需改 design. Co-Authored-By: Claude --- src/commands/form/_simplify.ts | 36 +++++++++++++++++++++++++-- tests/unit/field_normalize.test.ts | 40 ++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+), 2 deletions(-) create mode 100644 tests/unit/field_normalize.test.ts diff --git a/src/commands/form/_simplify.ts b/src/commands/form/_simplify.ts index 97b33f7..a5910df 100644 --- a/src/commands/form/_simplify.ts +++ b/src/commands/form/_simplify.ts @@ -56,9 +56,39 @@ export interface FullField { [key: string]: unknown } +/** + * 字段归一化: 把设计 HTML 格式 (field/fieldName/config:{required}) 映射成标准格式 (vmodel/label/required). + * 已标准格式直接返回; 透传 dataType/dataLength/options/relationTable/columnOptions/relationField/dictionaryType. + */ +function normalizeField(raw: unknown): SimpleField { + const f = raw as Record + // 已标准格式: 直接返回 + if (f.vmodel && f.label) return f as SimpleField + // 设计 HTML 格式: 转换 + if (f.field) { + const cfg = (f.config && typeof f.config === 'object' ? f.config : {}) as Record + return { + vmodel: String(f.field), + label: String(f.fieldName || f.field), + jnpfKey: String(f.jnpfKey || 'input'), + required: cfg.required === true, + ...(f.dataType ? { dataType: String(f.dataType) } : {}), + ...(f.dataLength ? { dataLength: String(f.dataLength) } : {}), + ...(f.options ? { options: f.options } : {}), + ...(f.relationTable ? { relationTable: String(f.relationTable) } : {}), + ...(f.columnOptions ? { columnOptions: f.columnOptions } : {}), + ...(f.relationField ? { relationField: String(f.relationField) } : {}), + ...(f.dictionaryType ? { dictionaryType: String(f.dictionaryType) } : {}), + } as SimpleField + } + // 未知格式: 原样抛错让原 throw 命中 + return f as SimpleField +} + /** * 把用户给的简化 fields 数组转成 JNPF web 标准的 __config__ schema. * 若 fields[0] 已含 __config__ 则原样透传 (完整格式). + * 支持两种简化格式: vmodel/label/required 或 field/fieldName/config:{required} (设计 HTML). */ export function wrapSimplifiedFields( fields: unknown[], @@ -75,8 +105,10 @@ export function wrapSimplifiedFields( return { wrapped: fields as FullField[], converted: 0, total: fields.length } } - const wrapped: FullField[] = fields.map((raw, i) => { - const f = raw as SimpleField + // 归一化: field/fieldName/config → vmodel/label/required + const normalized = fields.map(normalizeField) + + const wrapped: FullField[] = normalized.map((f, i) => { if (!f.vmodel) throw new Error(`fields[${i}] 缺少 vmodel 字段`) if (!f.jnpfKey) throw new Error(`fields[${i}] 缺少 jnpfKey 字段`) if (!f.label) throw new Error(`fields[${i}] 缺少 label 字段 (BS-31)`) diff --git a/tests/unit/field_normalize.test.ts b/tests/unit/field_normalize.test.ts new file mode 100644 index 0000000..6f79dfb --- /dev/null +++ b/tests/unit/field_normalize.test.ts @@ -0,0 +1,40 @@ +import { describe, it, expect } from 'vitest' +import { wrapSimplifiedFields } from '../../src/commands/form/_simplify.ts' + +describe('wrapSimplifiedFields — 字段格式归一化', () => { + it('支持 vmodel/label/required 格式', () => { + const r = wrapSimplifiedFields([ + { vmodel: 'f_name', label: '名称', jnpfKey: 'input', required: true }, + ], 'test_table') + expect(r.total).toBe(1) + expect(r.wrapped[0].__vModel__).toBe('f_name') + expect(r.wrapped[0].__config__.label).toBe('名称') + expect(r.wrapped[0].__config__.required).toBe(true) + }) + + it('支持 field/fieldName/config:{required} 格式 (设计 HTML 兼容)', () => { + const r = wrapSimplifiedFields([ + { field: 'f_competition_name', fieldName: '大赛名称', jnpfKey: 'input', config: { required: true } }, + ], 'jxcj_competition_info') + expect(r.total).toBe(1) + expect(r.wrapped[0].__vModel__).toBe('f_competition_name') + expect(r.wrapped[0].__config__.label).toBe('大赛名称') + expect(r.wrapped[0].__config__.required).toBe(true) + }) + + it('relationForm 字段两种格式都支持', () => { + const v = wrapSimplifiedFields([ + { vmodel: 'f_team', label: '团队', jnpfKey: 'relationForm', relationTable: 'base_user', columnOptions: [{ label: '姓名', value: 'realName' }] }, + ], 't') + const f = wrapSimplifiedFields([ + { field: 'f_team', fieldName: '团队', jnpfKey: 'relationForm', relationTable: 'base_user', columnOptions: [{ label: '姓名', value: 'realName' }] }, + ], 't') + expect(v.wrapped[0].relationTable).toBe('base_user') + expect(f.wrapped[0].relationTable).toBe('base_user') + }) + + it('必填校验: 两种格式缺关键字段都 throw', () => { + expect(() => wrapSimplifiedFields([{ vmodel: '', label: 'x', jnpfKey: 'input' }], 't')).toThrow(/vmodel/) + expect(() => wrapSimplifiedFields([{ field: '', fieldName: 'x', jnpfKey: 'input' }], 't')).toThrow(/vmodel/) + }) +})