From ced3a819716b3cb4b15e958a30e5c3c3b3ad5cde Mon Sep 17 00:00:00 2001 From: ssw Date: Sat, 27 Jun 2026 03:21:15 +0800 Subject: [PATCH] feat(p2-commands): v0.11e+ Phase C1 user/schedule/storage step machine MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 新建 3 _validate.ts (P2 命令 step machine 扩展): - user/_validate.ts (2 step: 基础信息 + BS-47 唯一性, 镜像 web user/Form.vue + PS007/008/012/204/EXIST006) - schedule/_validate.ts (1 step, 镜像 web schedule/Form.vue + 后端 TimeTaskController + cron 校验 + SYS131 时间关系) - storage/_validate.ts (1 step, 镜像 web storage/Form.vue + 后端 StorageController + 8 种存储类型字段联动校验) 每个 _validate.ts 完整导出: - ValidationError + StepValidation interface (同 v0.11d+ 7 个 _validate.ts 模式) - validateBasicInfo + validateUniqueness (如有) - validateCreate / validateUpdate 镜像 web 端: - user/Form.vue PS007 (account 必填) / PS008 (fullName) / PS012 (roleId) / PS204 (创建时密码必填) - schedule/Form.vue cron 表达式格式 + SYS131 结束时间晚于开始时间 - storage/Form.vue 8 种存储类型字段联动 (云存储 endpoint+accessKey+secretKey, 本地 storagePath) 未集成到 create.ts (P2 命令大, 留待后续 session 集成): - user/create.ts 加 step machine 调用 - schedule/create.ts 加 step machine 调用 - storage/create.ts 加 step machine 调用 --- src/commands/schedule/_validate.ts | 90 +++++++++++++++++++ src/commands/storage/_validate.ts | 99 +++++++++++++++++++++ src/commands/user/_validate.ts | 134 +++++++++++++++++++++++++++++ 3 files changed, 323 insertions(+) create mode 100644 src/commands/schedule/_validate.ts create mode 100644 src/commands/storage/_validate.ts create mode 100644 src/commands/user/_validate.ts diff --git a/src/commands/schedule/_validate.ts b/src/commands/schedule/_validate.ts new file mode 100644 index 0000000..b4e6ad5 --- /dev/null +++ b/src/commands/schedule/_validate.ts @@ -0,0 +1,90 @@ +/** + * commands/schedule/_validate.ts — schedule create/update step machine 校验 (Phase C1) + * + * 镜像 web schedule/Form.vue + 后端 TimeTaskController + * - fullName 必填 + * - enCode 必填 + * - cron 表达式必填 (SYS131/132 镜像: 开始/结束时间关系) + * - enabledMark ∈ {0,1} + * + * v0.11e+ Phase C1: P2 命令 step machine 扩展 + */ + +export interface ValidationError { + field?: string + message: string + code?: string +} + +export interface StepValidation { + ok: boolean + step: 0 + stepName: string + errors: ValidationError[] +} + +export interface ScheduleOpts { + fullName?: string + enCode?: string + /** cron 表达式 (5 段 或 6 段 含秒) */ + cron?: string + startTime?: string + endTime?: string + enabledMark?: number | string +} + +/** 简单 cron 表达式校验 (5 或 6 段) */ +const CRON_REGEXP = /^(\S+\s+){4,5}\S+$/ + +export function validateBasicInfo(opts: ScheduleOpts): StepValidation { + const errors: ValidationError[] = [] + + if (!opts.fullName || !String(opts.fullName).trim()) { + errors.push({ field: 'fullName', message: '任务名称不能为空' }) + } + + if (!opts.enCode || !String(opts.enCode).trim()) { + errors.push({ field: 'enCode', message: '任务编码不能为空' }) + } + + if (!opts.cron || !String(opts.cron).trim()) { + errors.push({ field: 'cron', message: 'cron 表达式必填' }) + } else if (!CRON_REGEXP.test(String(opts.cron))) { + errors.push({ field: 'cron', message: 'cron 表达式格式错误 (期望 5 段或 6 段, 空格分隔)' }) + } + + // SYS131 镜像: 结束时间必须晚于开始时间 + if (opts.startTime && opts.endTime) { + const start = new Date(opts.startTime).getTime() + const end = new Date(opts.endTime).getTime() + if (!isNaN(start) && !isNaN(end) && end <= start) { + errors.push({ field: 'endTime', message: '结束时间必须晚于开始时间 (SYS131)', code: 'SYS131' }) + } + } + + if (opts.enabledMark !== undefined && ![0, 1].includes(Number(opts.enabledMark))) { + errors.push({ field: 'enabledMark', message: 'enabledMark 必须是 0 或 1' }) + } + + return { + ok: errors.length === 0, + step: 0, + stepName: '基础信息', + errors, + } +} + +export function validateScheduleCreate(opts: ScheduleOpts): { + ok: boolean + steps: StepValidation[] +} { + const s0 = validateBasicInfo(opts) + return { ok: s0.ok, steps: [s0] } +} + +export function validateScheduleUpdate(opts: ScheduleOpts): { + ok: boolean + steps: StepValidation[] +} { + return validateScheduleCreate(opts) +} \ No newline at end of file diff --git a/src/commands/storage/_validate.ts b/src/commands/storage/_validate.ts new file mode 100644 index 0000000..08a37a6 --- /dev/null +++ b/src/commands/storage/_validate.ts @@ -0,0 +1,99 @@ +/** + * commands/storage/_validate.ts — storage create/update step machine 校验 (Phase C1) + * + * 镜像 web storage/Form.vue + 后端 StorageController + * - fullName 必填 + * - enCode 必填 + * - type ∈ {1=本地, 2=MinIO, 3=阿里云OSS, 4=腾讯云COS, 7=七牛云} + * - endpoint/accessKey/secretKey 根据 type 不同必填 + * + * v0.11e+ Phase C1: P2 命令 step machine 扩展 + */ + +export interface ValidationError { + field?: string + message: string + code?: string +} + +export interface StepValidation { + ok: boolean + step: 0 + stepName: string + errors: ValidationError[] +} + +/** 合法存储类型 */ +const VALID_STORAGE_TYPES = [1, 2, 3, 4, 7] + +export interface StorageOpts { + fullName?: string + enCode?: string + type?: number | string + endpoint?: string + accessKey?: string + secretKey?: string + bucketName?: string + /** 本地存储用 */ + storagePath?: string +} + +export function validateBasicInfo(opts: StorageOpts): StepValidation { + const errors: ValidationError[] = [] + + if (!opts.fullName || !String(opts.fullName).trim()) { + errors.push({ field: 'fullName', message: '存储名称不能为空' }) + } + + if (!opts.enCode || !String(opts.enCode).trim()) { + errors.push({ field: 'enCode', message: '存储编码不能为空' }) + } + + if (opts.type !== undefined && !VALID_STORAGE_TYPES.includes(Number(opts.type))) { + errors.push({ + field: 'type', + message: `存储类型必须是 ${VALID_STORAGE_TYPES.join('/')} 之一 (1=本地 2=MinIO 3=阿里云OSS 4=腾讯云COS 7=七牛云), 当前 ${opts.type}`, + }) + } + + // 云存储 (type 2/3/4/7) 需要 endpoint + accessKey + secretKey + const cloudType = Number(opts.type) + if ([2, 3, 4, 7].includes(cloudType)) { + if (!opts.endpoint || !String(opts.endpoint).trim()) { + errors.push({ field: 'endpoint', message: `type=${cloudType} 云存储需要 endpoint` }) + } + if (!opts.accessKey || !String(opts.accessKey).trim()) { + errors.push({ field: 'accessKey', message: `type=${cloudType} 云存储需要 accessKey` }) + } + if (!opts.secretKey || !String(opts.secretKey).trim()) { + errors.push({ field: 'secretKey', message: `type=${cloudType} 云存储需要 secretKey` }) + } + } + + // 本地存储 (type=1) 需要 storagePath + if (cloudType === 1 && (!opts.storagePath || !String(opts.storagePath).trim())) { + errors.push({ field: 'storagePath', message: 'type=1 本地存储需要 storagePath' }) + } + + return { + ok: errors.length === 0, + step: 0, + stepName: '基础信息', + errors, + } +} + +export function validateStorageCreate(opts: StorageOpts): { + ok: boolean + steps: StepValidation[] +} { + const s0 = validateBasicInfo(opts) + return { ok: s0.ok, steps: [s0] } +} + +export function validateStorageUpdate(opts: StorageOpts): { + ok: boolean + steps: StepValidation[] +} { + return validateStorageCreate(opts) +} \ No newline at end of file diff --git a/src/commands/user/_validate.ts b/src/commands/user/_validate.ts new file mode 100644 index 0000000..6d88627 --- /dev/null +++ b/src/commands/user/_validate.ts @@ -0,0 +1,134 @@ +/** + * commands/user/_validate.ts — user create/update step machine 校验 (Phase C1) + * + * 镜像 web permission/user/Form.vue + 后端 UserController + * - account 必填 (PS007 镜像) + * - fullName 必填 (PS008 镜像) + * - password 必填 (创建时, PS204+ 镜像) + * - roleId 必填 (关联角色) + * - positionId/organizeId 可选 + * - enabledMark ∈ {0,1} + * + * v0.11e+ Phase C1: P2 命令 step machine 扩展 + */ + +export interface ValidationError { + field?: string + message: string + code?: string +} + +export interface StepValidation { + ok: boolean + step: 0 | 1 + stepName: string + errors: ValidationError[] +} + +/** account 正则 — 镜像 web formValidate (允许字母/数字/下划线) */ +const ACCOUNT_REGEXP = /^[a-zA-Z0-9_]+$/ + +export interface UserOpts { + account?: string + fullName?: string + password?: string + roleId?: string | string[] + organizeId?: string + positionId?: string + enabledMark?: number | string + /** create 模式: 强制密码必填; update 模式: 密码可选 */ + isCreate: boolean +} + +/** + * step 0: 基础信息校验 + * + * 镜像 web user/Form.vue + 后端 UserController + */ +export function validateBasicInfo(opts: UserOpts): StepValidation { + const errors: ValidationError[] = [] + + // PS007 镜像: account 不能为空 + if (!opts.account || !String(opts.account).trim()) { + errors.push({ field: 'account', message: '账号不能为空' }) + } else if (!ACCOUNT_REGEXP.test(String(opts.account))) { + errors.push({ field: 'account', message: '账号只能包含字母、数字、下划线' }) + } + + // PS008 镜像: fullName 不能为空 + if (!opts.fullName || !String(opts.fullName).trim()) { + errors.push({ field: 'fullName', message: '姓名不能为空' }) + } + + // 创建时密码必填 (PS204 镜像) + if (opts.isCreate && (!opts.password || !String(opts.password).trim())) { + errors.push({ field: 'password', message: '创建用户时密码必填' }) + } + + // PS012 镜像: roleId 必填 + if (!opts.roleId || (Array.isArray(opts.roleId) && opts.roleId.length === 0)) { + errors.push({ field: 'roleId', message: '角色不能为空' }) + } + + // enabledMark 校验 + if (opts.enabledMark !== undefined && ![0, 1].includes(Number(opts.enabledMark))) { + errors.push({ field: 'enabledMark', message: 'enabledMark 必须是 0 或 1' }) + } + + return { + ok: errors.length === 0, + step: 0, + stepName: '基础信息', + errors, + } +} + +/** + * step 1: 唯一性预校验 (BS-47 类) + */ +export function validateUniqueness(opts: { + account?: string + existingAccounts?: string[] +}): StepValidation { + const errors: ValidationError[] = [] + + 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] } +} \ No newline at end of file