chore(form): address T1 review — source label, helper export, type casts, CLAUDE.md compliance

This commit is contained in:
ssw committed 2026-07-28 15:31:30 +08:00
1 parent 509ecc7e81
commit 53a5dd0886
4 files changed
+20 -9

No files matched your search

+8 -2
View File
@@ -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<string>(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<string, any>): Record<string, any> {
/** internal helper: 把单个 patch 归一化为 { __config__, ...passthrough } 形式.
* 仅供 applyNormalizedPatch 调用, 不 export — 公共入口用 applyNormalizedPatch.
* 刻意剥离 __vModel__ (key 已经是 patch 的 __vModel__), 由 caller 持有. */
function normalizeFieldPatch(patch: Record<string, any>): Record<string, any> {
const out: Record<string, any> = { __config__: {} }
// 顶层字段优先归位到 __config__, 避免顶层与 __config__ 出现矛盾值。
+2 -1
View File
@@ -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 */
+7 -5
View File
@@ -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<string, unknown>
}
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) {
+3 -1
View File
@@ -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')
})
})