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/) + }) +})