/** * commands/_encode_validator.ts — 共享 enCode 正则校验 (BS-56) * * 镜像 web packages/jnpf/utils/src/helpers/formValidate.ts:24-28 * 适用: form create / dict typeCreate / menu create / role create / workflow create / identity 等 * * v0.11h+ (2026-06-28): * - 统一 ENCODE_REGEXP (BS-56: 禁下划线, 禁首尾小数点) * - 增强错误信息: 明确指出"enCode 只能含英文/数字/小数点, 不能含下划线/连字符/中文" * - 自动建议别名: 把 `test_with_underscore` → `testwithunderscore` 让用户复制粘贴 * * Why: 历史 22 盲点根因 #B (AI 盲猜 enCode), BS-56 改进后让 AI 看到清晰规则 + 可用别名 */ export const ENCODE_REGEXP = /^[a-z0-9]((([a-z0-9])*)|(([a-z0-9]+|\.)*[a-z0-9]))$/i /** 镜像 web packages/jnpf/utils/src/helpers/formValidate.ts:33-36 * 中文/字母/数字, 禁特殊符号 */ export const FULLNAME_REGEXP = /^([一-龥a-z0-9])+$/i export interface EncodeValidationError { field: string message: string /** 镜像 web formValidate.ts:26-27 '只能输入英文、数字和小数点且小数点不能放在首尾' */ messageVague: string /** BS-56 增强: 自动建议的合法别名 (去下划线/连字符) */ suggestedAlias?: string code: string } /** * 校验 enCode 是否符合 JNPF formValidate 正则. * * 失败时返回清晰错误 + 建议别名; 通过返回 null. * * @param enCode 原始 enCode * @param field 字段名 (默认 'enCode') * @returns 错误对象 或 null (校验通过) */ export function validateEnCode(enCode: string | undefined, field = 'enCode'): EncodeValidationError | null { if (!enCode || !String(enCode).trim()) { return { field, message: '编码不能为空', messageVague: '编码不能为空', code: 'BS-56-EMPTY', } } const enc = String(enCode) if (enc.length > 50) { return { field, message: '编码最多 50 字符', messageVague: '编码最多 50 字符', code: 'BS-56-LEN', } } if (ENCODE_REGEXP.test(enc)) { return null } // 校验失败 — BS-56 增强: 清晰说明 + 建议别名 const suggested = suggestEnCode(enc) return { field, message: `BS-56: enCode 只能含英文、数字和小数点, 不能含下划线/连字符/中文. ` + `当前 enCode="${enc}" 非法. ` + (suggested && suggested !== enc ? `建议改成: --enCode="${suggested}" (自动去下划线/连字符)` : '请只保留字母/数字/小数点, 小数点不能放首尾'), messageVague: '只能输入英文、数字和小数点且小数点不能放在首尾', suggestedAlias: suggested, code: 'BS-56-FORMAT', } } /** * 自动建议合法的 enCode 别名: * - 去 `_` (下划线) * - 去 `-` (连字符) * - 去 ` ` (空格) * - 中文保留 (不主动改, 但 web formValidate 会拒) * - 小数点保留 (web 允许) */ export function suggestEnCode(enCode: string): string { return String(enCode) .replace(/[_\-]/g, '') // 去下划线/连字符 .replace(/\s+/g, '') // 去空格 } /** * v0.21+ (Round 12 fix, BS-新38): 自动 strip 的 enCode + log helper. * * 用于各 create 命令 action() 入口早期调用: * - 给 `round12-foo` → `round12foo` (去 `-`) * - 静默 strip, 但返回 wasStriped=true 让调用方打 WARN log * * 原因: Round 12 drift loop 5/8 模块 (form/dict/di/menu/role) 因 enCode 含 `-` 触发 BS-56 fail, * 必须手动重试. 加这个 helper, AI 一次提交就能跑通, 不用 retry. * * 注意: * - workflow 用 `r12.{enCode}` 含 dot (BS-56 允许), 不需要 strip * - bigscreen construct 没用 enCode, skip * - 只 strip `_` 和 `-`, 保留 `.` 和中文 (中文走 validateEnCode 拒) * * @param enCode 原始 enCode (字符串或 undefined) * @returns { value: 修正后 enCode, wasStriped: 是否做了修正, original: 原始值 } */ export function autoStripEnCode(enCode: string | undefined): { value: string wasStriped: boolean original: string } { const original = String(enCode || '') if (!original) { return { value: '', wasStriped: false, original } } const stripped = suggestEnCode(original) return { value: stripped, wasStriped: stripped !== original, original, } } /** * 校验 fullName (中文/字母/数字) */ export function validateFullName(fullName: string | undefined, field = 'fullName'): EncodeValidationError | null { if (!fullName || !String(fullName).trim()) { return { field, message: '名称不能为空', messageVague: '名称不能为空', code: 'BS-56-FULLNAME-EMPTY', } } const fn = String(fullName) if (fn.length > 100) { return { field, message: '名称最多 100 字符', messageVague: '名称最多 100 字符', code: 'BS-56-FULLNAME-LEN', } } if (FULLNAME_REGEXP.test(fn)) { return null } return { field, message: 'BS-56: 名称只能含中文/字母/数字, 不能含特殊符号. 当前名称含非法字符', messageVague: '名称不能含有特殊符号', code: 'BS-56-FULLNAME-FORMAT', } } /** * v0.21+ R14 fix (BS-新52): 自动 strip 的 fullName + log helper. * * 跟 autoStripEnCode 平行: fullName 含 FULLNAME_REGEXP 禁的字符 (空格/半角括号/全角括号/斜杠/引号等) * 自动剥除, 让 AI 一次提交跑通不用 retry. * * R13 现象: `round13 客户管理记录表(集成助手)` 含半角空格 + 全角括号 → BS-56 step0 "名称不能含有特殊符号". * 修法: autoStrip 后 `round13客户管理记录表集成助手` 通过校验, AI retry 减少一次. * * strip 规则 (按优先级): * - 去空格 (半角+全角) * - 去括号 (半角 () + 全角 ()+ [] + 【】 + {} + {}) * - 去引号 (半角 " ' + 全角 “” ‘’) * - 去斜杠 (/ \ + 全角 / \) * - 去连字符 (- _) — 注意保留小数点 (.) 不动 * - 去其他常见特殊符号 (.,;:!? — 保留句号) * * @param fullName 原始 fullName * @returns { value, wasStriped, original } */ export function autoStripFullName(fullName: string | undefined): { value: string wasStriped: boolean original: string } { const original = String(fullName || '') if (!original) { return { value: '', wasStriped: false, original } } const stripped = original .replace(/[\s ]/g, '') // 半角空格 + 全角空格 .replace(/[()()\[\]【】{}{}]/g, '') // 半角+全角括号 .replace(/["'“”‘’]/g, '') // 半角+全角引号 .replace(/[/\/]/g, '') // 半角+全角斜杠 .replace(/[-_]/g, '') // 半角连字符+下划线 .replace(/[,;:!?、,;:!?]/g, '') // 半角+全角中英文标点 (保留 .) return { value: stripped, wasStriped: stripped !== original, original, } } // v0.15+ (P2-5, 2026-07-02): 删 BS-108 adaptEnCodeForScope/detectEnCodeScope dead code // - 整个 codebase 零引用 (grep 只在自己文件内) // - BS-56 formValidate 正则已经统一 form/dict/menu/di, 不需要 scope 区分 // - audit FINDING-S1: '要么接入 form/dict/menu/di 要么删', 选删 (form 走 web 端后端校验)