From 53a5dd088622e60d32cc73d609488ddd4bf1a543 Mon Sep 17 00:00:00 2001 From: ssw Date: Tue, 28 Jul 2026 15:31:30 +0800 Subject: [PATCH] =?UTF-8?q?chore(form):=20address=20T1=20review=20?= =?UTF-8?q?=E2=80=94=20source=20label,=20helper=20export,=20type=20casts,?= =?UTF-8?q?=20CLAUDE.md=20compliance?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/commands/form/_schema_normalize.ts | 10 ++++++++-- src/commands/form/_validate.ts | 3 ++- src/commands/form/update.ts | 12 +++++++----- tests/unit/form_schema_normalize.test.ts | 4 +++- 4 files changed, 20 insertions(+), 9 deletions(-) diff --git a/src/commands/form/_schema_normalize.ts b/src/commands/form/_schema_normalize.ts index 281080a..b682da0 100644 --- a/src/commands/form/_schema_normalize.ts +++ b/src/commands/form/_schema_normalize.ts @@ -1,5 +1,5 @@ /** - * form schema 归一化 (Task 1, 2026-07-28) + * form schema 归一化 (webapi contract hardening) * 镜像 web formUtils.buildFormData: 把“输入 patch”变成 web 渲染用的最终 schema. * - 顶层字段 (dataType/required/...) → __config__ 深合并 * - 控件切换时清理“上一种控件”专属字段 (relationForm → 普通控件) @@ -23,9 +23,15 @@ const CONFIG_KEYS = [ ] as const const CONFIG_KEY_SET = new Set(CONFIG_KEYS) const RELATION_FORM_KEYS = ['modelId', 'propsValue', 'relationField', 'showField', 'relationTable'] +/** 控件禁止 required 的集合 — mirrors web component capability matrix + * (FormGenerator/src/helper/componentMap.ts: 这些控件元数据里无 `required` 字段, + * 即便配置 required 也会被前端 web 渲染时忽略, 并触发控制台 warning). */ const NO_REQUIRE_JNPF_KEYS = new Set(['divider', 'button', 'alert', 'collapse', 'card']) -export function normalizeFieldPatch(patch: Record): Record { +/** internal helper: 把单个 patch 归一化为 { __config__, ...passthrough } 形式. + * 仅供 applyNormalizedPatch 调用, 不 export — 公共入口用 applyNormalizedPatch. + * 刻意剥离 __vModel__ (key 已经是 patch 的 __vModel__), 由 caller 持有. */ +function normalizeFieldPatch(patch: Record): Record { const out: Record = { __config__: {} } // 顶层字段优先归位到 __config__, 避免顶层与 __config__ 出现矛盾值。 diff --git a/src/commands/form/_validate.ts b/src/commands/form/_validate.ts index a1eea6b..ccdfcea 100644 --- a/src/commands/form/_validate.ts +++ b/src/commands/form/_validate.ts @@ -33,7 +33,8 @@ import { ValidationError } from '../_error_codes.ts' import { ENCODE_REGEXP, FULLNAME_REGEXP, validateEnCode, validateFullName } from '../_encode_validator.ts' import { log } from '../../core/log.ts' -// Task 1 (2026-07-28): create 路径共用 form update 的 strict 校验 (仅 re-export, 避免循环依赖) +// webapi hardening: create 路径共用 form update 的 strict 校验 (re-export, 避免反向引用 _validate.ts) +// TODO: create 路径真正接入 (Task 3) 后再去掉此桥接 export { validateStrictSchema } from './_schema_normalize.ts' /** upload folder name 正则 — 镜像 Generator.getData() line 295 */ diff --git a/src/commands/form/update.ts b/src/commands/form/update.ts index bdfbc8a..896c3d8 100644 --- a/src/commands/form/update.ts +++ b/src/commands/form/update.ts @@ -14,7 +14,7 @@ import { initWebapi, loadVisualDev } from '../../webapi/visualDev.ts' import { dirtyCheck, dirtyCheckFailAndExit, DirtyCheckError, logDirtyCheckSkip, buildDirtyCheckSkipFix } from '../_dirty_check.ts' import { enhanceFromBackend, formatErrorLine } from '../_error_codes.ts' import { setupOutputFilter, addOutputOptions, wrapCommand } from '../_output_filter.ts' // v2.1+ 输出过滤; v2.2+ wrapCommand 收口 5c -import { applyNormalizedPatch, validateStrictSchema } from './_schema_normalize.ts' // Task 1 (2026-07-28): schema 归一化 + 严格校验 +import { applyNormalizedPatch, validateStrictSchema, type FormField } from './_schema_normalize.ts' // schema 归一化 + 严格校验 (webapi hardening) interface Opts { id: string @@ -335,13 +335,15 @@ export function buildFormUpdateCommand(): Command { formData = fd as Record } const existingFields = Array.isArray(formData.fields) ? (formData.fields as unknown[]) : [] - const merged = applyNormalizedPatch(existingFields as any[], parsed as any[]) as unknown[] + // trust boundary: JSON.parse 的 formData.fields + 用户 --fields 走 DacliError 兜底, 业务层用 FormField 接口约束 + const merged = applyNormalizedPatch(existingFields as FormField[], parsed as FormField[]) as unknown[] formData.fields = merged - // Task 1 (2026-07-28): 严格 schema 校验, 阻断非法字段 (缺 jnpfKey / 控件能力冲突) - const strict = validateStrictSchema(merged as any[]) + // 严格 schema 校验, 阻断非法字段 (缺 jnpfKey / 控件能力冲突). 失败 → DacliError 抛出被 wrapCommand 顶层 catch + const strict = validateStrictSchema(merged as FormField[]) if (!strict.ok) { for (const err of strict.errors) log.error(` ✗ ${err}`) - throw new DacliError('FORM_SCHEMA_INVALID', -1, JSON.stringify({ id: opts.id, errors: strict.errors, source: 'applyNormalizedPatch' })) + // DacliError 2nd 参数: HTTP 业务码 -1=本地校验失败 (非后端 HTTP 返回, raw 段塞上下文) + throw new DacliError('FORM_SCHEMA_INVALID', -1, JSON.stringify({ id: opts.id, errors: strict.errors, source: 'validateStrictSchema' })) } // v2.4+ (2026-07-24): strict 校验, relationForm 4 字段必填 (TD8 smoke test 命中) if (opts.strict !== false) { diff --git a/tests/unit/form_schema_normalize.test.ts b/tests/unit/form_schema_normalize.test.ts index e5bf5a9..6985df9 100644 --- a/tests/unit/form_schema_normalize.test.ts +++ b/tests/unit/form_schema_normalize.test.ts @@ -1,5 +1,5 @@ import { describe, expect, it } from 'vitest' -import { normalizeFieldPatch, applyNormalizedPatch, validateStrictSchema } from '../../src/commands/form/_schema_normalize.ts' +import { applyNormalizedPatch, validateStrictSchema } from '../../src/commands/form/_schema_normalize.ts' describe('form schema normalize (Task 1)', () => { const existing = [{ @@ -43,5 +43,7 @@ describe('form schema normalize (Task 1)', () => { it('validateStrictSchema: required 与控件 capability 矛盾时报错 (如 divider 不能 required)', () => { const r = validateStrictSchema([{ __vModel__: 'f_x', __config__: { jnpfKey: 'divider', required: true } }]) expect(r.ok).toBe(false) + expect(r.errors[0]).toContain('f_x') + expect(r.errors[0]).toContain('divider') }) })