refactor: 使用 Zod 校验扩展配置
This commit is contained in:
1 parent
8baa042255
commit
600b1d64b4
3 files changed
+53
-33
No files matched your search
@@ -13,5 +13,8 @@
|
|||||||
"lint": "eslint .",
|
"lint": "eslint .",
|
||||||
"typecheck": "tsc -p tsconfig.json --noEmit",
|
"typecheck": "tsc -p tsconfig.json --noEmit",
|
||||||
"clean": "rimraf dist"
|
"clean": "rimraf dist"
|
||||||
|
},
|
||||||
|
"dependencies": {
|
||||||
|
"zod": "^3.25.0"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,43 +1,51 @@
|
|||||||
|
import { z } from "zod";
|
||||||
|
|
||||||
export const validationPackageName = "personal-brand-validation";
|
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 };
|
export type ValidationIssue = { path: string; message: string };
|
||||||
|
|
||||||
function isRecord(value: unknown): value is Record<string, unknown> {
|
function issues(result: z.SafeParseReturnType<unknown, unknown>): ValidationIssue[] {
|
||||||
return typeof value === "object" && value !== null && !Array.isArray(value);
|
return result.success
|
||||||
|
? []
|
||||||
|
: result.error.issues.map((issue) => ({ path: issue.path.join("."), message: issue.message }));
|
||||||
}
|
}
|
||||||
|
|
||||||
export function validateFieldSnapshot(value: unknown): ValidationIssue[] {
|
export function validateFieldSnapshot(value: unknown): ValidationIssue[] {
|
||||||
if (!Array.isArray(value)) return [{ path: "fieldsSnapshot", message: "必须是数组" }];
|
return issues(fieldSnapshotSchema.safeParse(value));
|
||||||
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[] {
|
export function validateOfferingConfig(value: unknown): ValidationIssue[] {
|
||||||
if (value === undefined || value === null) return [];
|
return value === undefined || value === null ? [] : issues(offeringConfigSchema.safeParse(value));
|
||||||
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 [];
|
|
||||||
}
|
}
|
||||||
Generated
+10
-1
@@ -135,7 +135,11 @@ importers:
|
|||||||
|
|
||||||
packages/types: {}
|
packages/types: {}
|
||||||
|
|
||||||
packages/validation: {}
|
packages/validation:
|
||||||
|
dependencies:
|
||||||
|
zod:
|
||||||
|
specifier: ^3.25.0
|
||||||
|
version: 3.25.76
|
||||||
|
|
||||||
packages:
|
packages:
|
||||||
|
|
||||||
@@ -1964,6 +1968,9 @@ packages:
|
|||||||
resolution: {integrity: sha512-rVksvsnNCdJ/ohGc6xgPwyN8eheCxsiLM8mxuE/t/mOVqJewPuO1miLpTHQiRgTKCLexL4MeAFVagts7HmNZ2Q==}
|
resolution: {integrity: sha512-rVksvsnNCdJ/ohGc6xgPwyN8eheCxsiLM8mxuE/t/mOVqJewPuO1miLpTHQiRgTKCLexL4MeAFVagts7HmNZ2Q==}
|
||||||
engines: {node: '>=10'}
|
engines: {node: '>=10'}
|
||||||
|
|
||||||
|
zod@3.25.76:
|
||||||
|
resolution: {integrity: sha512-gzUt/qt81nXsFGKIFcC3YnfEAx5NkunCfnDlvuBSSFS02bcXu4Lmea0AFIUwbLWxWPx3d9p8S5QoaujKcNQxcQ==}
|
||||||
|
|
||||||
snapshots:
|
snapshots:
|
||||||
|
|
||||||
'@ant-design/colors@8.0.1':
|
'@ant-design/colors@8.0.1':
|
||||||
@@ -3748,3 +3755,5 @@ snapshots:
|
|||||||
word-wrap@1.2.5: {}
|
word-wrap@1.2.5: {}
|
||||||
|
|
||||||
yocto-queue@0.1.0: {}
|
yocto-queue@0.1.0: {}
|
||||||
|
|
||||||
|
zod@3.25.76: {}
|
||||||
Reference in new issue
Block a user