fix: 修复系统一致性与安全问题

This commit is contained in:
que01 committed 2026-10-14 17:36:46 +08:00
1 parent aaeec14733
commit f158f906d5
19 files changed
+194 -34

No files matched your search

+39 -12
View File
@@ -21,6 +21,12 @@ const inquiryFieldOptionSchema = z.object({
value: z.string().min(1),
});
const visibilityRuleSchema = z.object({
field: z.string().regex(/^[a-z][a-zA-Z0-9_]*$/),
operator: z.enum(["equals", "not-equals", "exists", "in"]),
value: z.union([z.string(), z.number(), z.boolean(), z.array(z.string())]).optional(),
});
export const inquiryFieldSchema = z
.object({
// 字段 key 允许 snake_case 和 camelCase,模板字段通常使用 camelCase。
@@ -29,6 +35,7 @@ export const inquiryFieldSchema = z
label: z.string().min(1),
required: z.boolean().optional(),
options: z.array(inquiryFieldOptionSchema).optional(),
visibleWhen: z.array(visibilityRuleSchema).optional(),
})
.passthrough()
.superRefine((field, context) => {
@@ -41,19 +48,39 @@ export const inquiryFieldSchema = z
}
});
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);
const fieldVisibilityRulesRefinement = (
fields: Array<z.infer<typeof inquiryFieldSchema>>,
context: z.RefinementCtx,
) => {
fields.forEach((field, fieldIndex) => {
(field.visibleWhen ?? []).forEach((rule, ruleIndex) => {
if (!fields.some((target) => target.key === rule.field)) {
context.addIssue({
code: z.ZodIssueCode.custom,
path: [fieldIndex, "visibleWhen", ruleIndex, "field"],
message: "可见性规则引用的字段不存在",
});
}
});
});
});
};
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);
});
})
.superRefine(fieldVisibilityRulesRefinement);
export const offeringConfigSchema = z
.object({