feat: 增加扩展配置校验器

This commit is contained in:
que01 committed 2026-09-21 16:03:03 +08:00
1 parent 29d14378a7
commit 8baa042255
7 files changed
+85 -32

No files matched your search

+42
View File
@@ -1 +1,43 @@
export const validationPackageName = "personal-brand-validation";
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);
}
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;
}
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 [];
}