diff --git a/packages/validation/package.json b/packages/validation/package.json index a06dd95..9002787 100644 --- a/packages/validation/package.json +++ b/packages/validation/package.json @@ -13,5 +13,8 @@ "lint": "eslint .", "typecheck": "tsc -p tsconfig.json --noEmit", "clean": "rimraf dist" + }, + "dependencies": { + "zod": "^3.25.0" } } diff --git a/packages/validation/src/index.ts b/packages/validation/src/index.ts index e63301a..cdc8b69 100644 --- a/packages/validation/src/index.ts +++ b/packages/validation/src/index.ts @@ -1,43 +1,51 @@ +import { z } from "zod"; + export const validationPackageName = "personal-brand-validation"; +export const inquiryFieldSchema = z + .object({ + key: z.string().min(1), + type: z.string().min(1), + label: z.string().min(1), + required: z.boolean().optional(), + }) + .passthrough(); + +export const fieldSnapshotSchema = z.array(inquiryFieldSchema).superRefine((fields, context) => { + const seen = new Set(); + fields.forEach((field, index) => { + if (seen.has(field.key)) { + context.addIssue({ + code: z.ZodIssueCode.custom, + path: [index, "key"], + message: "字段 key 不能重复", + }); + } + seen.add(field.key); + }); +}); + +export const offeringConfigSchema = z + .object({ + schemaVersion: z.string().optional(), + sections: z.array(z.unknown()).optional(), + media: z.array(z.unknown()).optional(), + cta: z.record(z.unknown()).optional(), + }) + .passthrough(); + export type ValidationIssue = { path: string; message: string }; -function isRecord(value: unknown): value is Record { - return typeof value === "object" && value !== null && !Array.isArray(value); +function issues(result: z.SafeParseReturnType): ValidationIssue[] { + return result.success + ? [] + : result.error.issues.map((issue) => ({ path: issue.path.join("."), message: issue.message })); } export function validateFieldSnapshot(value: unknown): ValidationIssue[] { - if (!Array.isArray(value)) return [{ path: "fieldsSnapshot", message: "必须是数组" }]; - const issues: ValidationIssue[] = []; - const keys = new Set(); - for (const [index, field] of value.entries()) { - if (!isRecord(field)) { - issues.push({ path: `fieldsSnapshot.${index}`, message: "字段必须是对象" }); - continue; - } - for (const key of ["key", "type", "label"]) { - if (typeof field[key] !== "string" || !field[key]) - issues.push({ path: `fieldsSnapshot.${index}.${key}`, message: "必须是非空字符串" }); - } - if (typeof field.key === "string") { - if (keys.has(field.key)) - issues.push({ path: `fieldsSnapshot.${index}.key`, message: "字段 key 不能重复" }); - keys.add(field.key); - } - if (field.required !== undefined && typeof field.required !== "boolean") - issues.push({ path: `fieldsSnapshot.${index}.required`, message: "必须是布尔值" }); - } - return issues; + return issues(fieldSnapshotSchema.safeParse(value)); } export function validateOfferingConfig(value: unknown): ValidationIssue[] { - if (value === undefined || value === null) return []; - if (!isRecord(value)) return [{ path: "config", message: "必须是对象" }]; - if (value.schemaVersion !== undefined && typeof value.schemaVersion !== "string") - return [{ path: "config.schemaVersion", message: "必须是字符串" }]; - for (const key of ["sections", "media"]) { - if (value[key] !== undefined && !Array.isArray(value[key])) - return [{ path: `config.${key}`, message: "必须是数组" }]; - } - return []; + return value === undefined || value === null ? [] : issues(offeringConfigSchema.safeParse(value)); } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index fa38984..0bf42b6 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -135,7 +135,11 @@ importers: packages/types: {} - packages/validation: {} + packages/validation: + dependencies: + zod: + specifier: ^3.25.0 + version: 3.25.76 packages: @@ -1964,6 +1968,9 @@ packages: resolution: {integrity: sha512-rVksvsnNCdJ/ohGc6xgPwyN8eheCxsiLM8mxuE/t/mOVqJewPuO1miLpTHQiRgTKCLexL4MeAFVagts7HmNZ2Q==} engines: {node: '>=10'} + zod@3.25.76: + resolution: {integrity: sha512-gzUt/qt81nXsFGKIFcC3YnfEAx5NkunCfnDlvuBSSFS02bcXu4Lmea0AFIUwbLWxWPx3d9p8S5QoaujKcNQxcQ==} + snapshots: '@ant-design/colors@8.0.1': @@ -3748,3 +3755,5 @@ snapshots: word-wrap@1.2.5: {} yocto-queue@0.1.0: {} + + zod@3.25.76: {}