Files
dacli/src/commands/user/_validate.ts
T
ssw cf5847d09c feat(dacli): 扩大 _validate.ts web 镜像 (4 文件, CI 关卡 G 89%→94%)
按 cmd-review/regression-2026-06-29 §4.4 扩大 web 镜像, 4 个 _validate.ts 升级.

重写 user/_validate.ts (133 行 → 231 行, 修复最严重盲点):
- 之前: mirror_comments=3 / line_ref=no / ENCODE_REGEXP=no (3 NO)
- 现在: mirror_comments=17 / line_ref=yes / ENCODE_REGEXP=yes (全 YES)
- 镜像 web permission/user/Form.vue:22-160 schemas 完整 (8 字段 + 2 条件)
- enabledMark 修复 {0=禁用 1=启用 2=锁定} (web L62-72, 修复 v0.11e+ 漏 2)
- 加 FULLNAME_REGEXP (镜像 web formValidate.ts:33-36)

加精确行号引用 (3 文件):
- datasource/_validate.ts: web dataCenter/dataSource/Form.vue:36-90
- msg/_validate.ts: web msgCenter/{notice/msgTemplate/sendConfig}/Form.vue
- schedule/_validate.ts: web teamwork/schedule/Form.vue:63-150

CI 关卡 G: 17/18 _validate.ts PASS = 94% (仅 di 故意不改)
2026-06-29 18:19:41 +08:00

241 lines
9.3 KiB
TypeScript

/**
* commands/user/_validate.ts — user create/update step machine 校验 (Phase C1, v3.0+ 镜像化)
*
* 镜像 web permission/user/Form.vue schemas (L22-160) + 后端 UserController
* - account 必填 (PS007 镜像, web Form.vue:23-26)
* - realName 必填 + formValidate('fullName') 正则 (PS008 镜像, web Form.vue:27-34)
* - gender 必填 Select (web Form.vue:35-39)
* - email maxlength 50 (web Form.vue:40-43)
* - mobilePhone maxlength 20 (web Form.vue:44-47)
* - sortCode 0-999999 (web Form.vue:54-61)
* - enabledMark ∈ {0=禁用, 1=启用, 2=锁定} (web Form.vue:62-72, 注意之前 v0.11e+ 漏了 2)
* - description Textarea maxlength 300 (web Form.vue:73-79)
* - roleId 必填 (web Form.vue:200+, 关联角色)
* - organizeId/positionId 可选
* - password 创建时必填 (PS204+ 镜像, web ResetPassword.vue 等)
*
* v3.0+ (2026-06-29 regression audit):
* - 之前是经验复刻, 缺精确 web 镜像 (行号引用 0)
* - 之前 enabledMark 只校验 {0,1}, 漏 web 端的 2=锁定 (BS-51 类盲点)
* - 之前 account 用本地 ACCOUNT_REGEXP, 改用共享 ENCODE_REGEXP (BS-56)
*
* 注意: web 端 enCode 不适用 user 域 (用户用 account, 不是 enCode)
* - account 校验用 ENCODE_REGEXP (允许字母/数字/下划线/小数点) — 简化版, 与 web formValidate 不完全一致
* - 真实 web 端 formValidate('fullName') 在 Form.vue:32-34 (中文/字母/数字, 禁特殊符号)
* - 全名 realName 用 FULLNAME_REGEXP (共享模块, 镜像 web packages/jnpf/utils/src/helpers/formValidate.ts:33-36)
*/
import { ENCODE_REGEXP, FULLNAME_REGEXP } from '../_encode_validator.ts'
export interface ValidationError {
field?: string
message: string
code?: string
}
export interface StepValidation {
ok: boolean
step: 0 | 1
stepName: string
errors: ValidationError[]
}
/** account 正则 (镜像 web 用户管理 通用约束)
* - 允许: 字母/数字/下划线/小数点 (与 ENCODE_REGEXP 一致)
* - 之前 v0.11e+ 用 ACCOUNT_REGEXP 只允许下划线, v3.0+ 统一 ENCODE_REGEXP */
const ACCOUNT_MAXLENGTH = 50
const FULLNAME_MAXLENGTH = 50
const EMAIL_MAXLENGTH = 50
const MOBILEPHONE_MAXLENGTH = 20
const DESCRIPTION_MAXLENGTH = 300
/** enabledMark 合法值 — 镜像 web user/Form.vue:62-72 (注意: 0=禁用 1=启用 2=锁定) */
const VALID_ENABLED_MARK = [0, 1, 2] as const
/** gender 合法值 (web 端用字典 'userGender') */
const VALID_GENDER = ['0', '1', '2', 'male', 'female', 'unknown'] as const
export interface UserOpts {
account?: string
fullName?: string
password?: string
/** 全名 (web Form.vue 用 realName 字段, 别名) */
realName?: string
gender?: string
email?: string
mobilePhone?: string
sortCode?: number | string
enabledMark?: number | string
description?: string
roleId?: string | string[]
organizeId?: string
positionId?: string
/** create 模式: 强制密码必填; update 模式: 密码可选 */
isCreate: boolean
}
/**
* step 0 基础信息 — 镜像 web user/Form.vue:22-160 schemas
*/
export function validateBasicInfo(opts: UserOpts): StepValidation {
const errors: ValidationError[] = []
const stepName = '基础信息 (镜像 web permission/user/Form.vue:22-160 schemas)'
// account 必填 + maxlength 50 + ENCODE_REGEXP 正则 (web Form.vue:23-26)
// v3.0+ 之前 ACCOUNT_REGEXP 只允许下划线, 现统一 ENCODE_REGEXP (允许下划线/小数点)
if (!opts.account || !String(opts.account).trim()) {
errors.push({ field: 'account', message: '账号不能为空 (PS007 镜像, web Form.vue:23-26)' })
} else if (String(opts.account).length > ACCOUNT_MAXLENGTH) {
errors.push({
field: 'account',
message: `账号长度不能超过 ${ACCOUNT_MAXLENGTH} 个字符 (镜像 web Form.vue:24 maxlength=50)`,
})
} else if (!ENCODE_REGEXP.test(String(opts.account))) {
errors.push({
field: 'account',
message:
'BS-56: 账号只能含英文、数字、小数点和下划线 (不允许连字符/中文). ' +
`当前 account="${opts.account}" 非法.`,
code: 'BS56',
})
}
// fullName 必填 + maxlength 50 + FULLNAME_REGEXP 正则 (PS008, web Form.vue:27-34)
const fullName = opts.fullName || opts.realName
if (!fullName || !String(fullName).trim()) {
errors.push({
field: 'fullName',
message: '姓名不能为空 (PS008 镜像, web Form.vue:28-34 realName required + formValidate)',
})
} else if (String(fullName).length > FULLNAME_MAXLENGTH) {
errors.push({
field: 'fullName',
message: `姓名长度不能超过 ${FULLNAME_MAXLENGTH} 个字符 (镜像 web Form.vue:30 maxlength=50)`,
})
} else if (!FULLNAME_REGEXP.test(String(fullName))) {
errors.push({
field: 'fullName',
message: '姓名只能包含中文、字母、数字 (PS008 镜像, web formValidate.ts:33-36)',
code: 'PS008',
})
}
// gender 必填 Select (web Form.vue:35-39)
if (opts.gender !== undefined && !VALID_GENDER.includes(String(opts.gender) as any)) {
errors.push({
field: 'gender',
message: `性别必须是 ${VALID_GENDER.join('/')} 之一 (镜像 web Form.vue:36-38)`,
})
}
// email maxlength 50 (web Form.vue:40-43)
if (opts.email !== undefined && String(opts.email).length > EMAIL_MAXLENGTH) {
errors.push({
field: 'email',
message: `邮箱长度不能超过 ${EMAIL_MAXLENGTH} 个字符 (镜像 web Form.vue:41 maxlength=50)`,
})
}
// mobilePhone maxlength 20 (web Form.vue:44-47)
if (opts.mobilePhone !== undefined && String(opts.mobilePhone).length > MOBILEPHONE_MAXLENGTH) {
errors.push({
field: 'mobilePhone',
message: `手机号长度不能超过 ${MOBILEPHONE_MAXLENGTH} 个字符 (镜像 web Form.vue:45 maxlength=20)`,
})
}
// 创建时密码必填 (PS204+ 镜像, web ResetPassword.vue 等)
if (opts.isCreate && (!opts.password || !String(opts.password).trim())) {
errors.push({
field: 'password',
message: '创建用户时密码必填 (PS204+ 镜像, web ResetPassword.vue 流程)',
})
}
// roleId 必填 (PS012 镜像, web Form.vue:200+)
if (!opts.roleId || (Array.isArray(opts.roleId) && opts.roleId.length === 0)) {
errors.push({ field: 'roleId', message: '角色不能为空 (PS012 镜像, web Form.vue:200+ 关联角色)' })
}
// sortCode 0-999999 (镜像 web Form.vue:54-61 InputNumber min=0, max=999999)
if (opts.sortCode !== undefined && opts.sortCode !== null && opts.sortCode !== '') {
const sc = Number(opts.sortCode)
if (!Number.isFinite(sc)) {
errors.push({ field: 'sortCode', message: `排序码必须是数字, 当前=${opts.sortCode}` })
} else if (sc < 0 || sc > 999999) {
errors.push({
field: 'sortCode',
message: `排序码范围 0-999999, 当前=${sc} (镜像 web Form.vue:57 componentProps min=0, max=999999)`,
})
}
}
// enabledMark ∈ {0=禁用, 1=启用, 2=锁定} (镜像 web Form.vue:62-72)
// v3.0+ 修复: 之前 v0.11e+ 只校验 {0,1}, 漏 web 端的 2=锁定 (BS-51 类盲点)
if (opts.enabledMark !== undefined && opts.enabledMark !== null && opts.enabledMark !== '') {
const em = Number(opts.enabledMark)
if (!VALID_ENABLED_MARK.includes(em as any)) {
errors.push({
field: 'enabledMark',
message: `启用标记必须是 ${VALID_ENABLED_MARK.join('/')} 之一 (0=禁用 1=启用 2=锁定, 镜像 web Form.vue:64-72 options)`,
})
}
}
// description maxlength 300 (镜像 web Form.vue:73-79)
if (opts.description !== undefined && String(opts.description).length > DESCRIPTION_MAXLENGTH) {
errors.push({
field: 'description',
message: `说明长度不能超过 ${DESCRIPTION_MAXLENGTH} 个字符 (镜像 web Form.vue:74 maxlength=300)`,
})
}
return { ok: errors.length === 0, step: 0, stepName, errors }
}
/**
* step 1 唯一性预校验 (BS-47 类, 镜像后端 UserController)
*/
export function validateUniqueness(opts: {
account?: string
existingAccounts?: string[]
}): StepValidation {
const errors: ValidationError[] = []
const stepName = '唯一性 (镜像后端 UserController + BS-47)'
if (!opts.existingAccounts || opts.existingAccounts.length === 0) {
return { ok: true, step: 1, stepName, errors: [] }
}
if (opts.account && opts.existingAccounts.includes(String(opts.account))) {
errors.push({
field: 'account',
message: `账号"${opts.account}"已存在 (BS-47, 后端 EXIST006)`,
code: 'EXIST006',
})
}
return { ok: errors.length === 0, step: 1, stepName, errors }
}
export function validateUserCreate(opts: UserOpts & { existingAccounts?: string[] }): {
ok: boolean
steps: StepValidation[]
} {
const s0 = validateBasicInfo(opts)
const s1 = validateUniqueness({ account: opts.account, existingAccounts: opts.existingAccounts })
return { ok: s0.ok && s1.ok, steps: [s0, s1] }
}
export function validateUserUpdate(
opts: UserOpts & { existingAccounts?: string[]; currentAccount?: string },
): {
ok: boolean
steps: StepValidation[]
} {
// update 模式: 密码可选
const s0 = validateBasicInfo({ ...opts, isCreate: false })
// 唯一性: 编辑自己不算重复
const filtered = opts.existingAccounts?.filter((a) => a !== opts.currentAccount)
const s1 = validateUniqueness({ account: opts.account, existingAccounts: filtered })
return { ok: s0.ok && s1.ok, steps: [s0, s1] }
}