refactor: 使用 Zod 校验扩展配置

This commit is contained in:
que01 committed 2026-09-21 16:11:34 +08:00
1 parent 8baa042255
commit 600b1d64b4
3 files changed
+53 -33

No files matched your search

+40 -32
View File
@@ -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<string>();
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<string, unknown> {
return typeof value === "object" && value !== null && !Array.isArray(value);
function issues(result: z.SafeParseReturnType<unknown, unknown>): 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<string>();
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));
}