feat: 强化动态表单字段校验
This commit is contained in:
1 parent
ef0aa169a6
commit
d6c47ce143
7 files changed
+94
-4
No files matched your search
@@ -2,14 +2,43 @@ import { z } from "zod";
|
||||
|
||||
export const validationPackageName = "personal-brand-validation";
|
||||
|
||||
const inquiryFieldTypeSchema = z.enum([
|
||||
"text",
|
||||
"tel",
|
||||
"phone",
|
||||
"number",
|
||||
"select",
|
||||
"radio",
|
||||
"multi-select",
|
||||
"textarea",
|
||||
"date",
|
||||
"image",
|
||||
"url",
|
||||
]);
|
||||
|
||||
const inquiryFieldOptionSchema = z.object({
|
||||
label: z.string().min(1),
|
||||
value: z.string().min(1),
|
||||
});
|
||||
|
||||
export const inquiryFieldSchema = z
|
||||
.object({
|
||||
key: z.string().min(1),
|
||||
type: z.string().min(1),
|
||||
key: z.string().regex(/^[a-z][a-z0-9_]*$/),
|
||||
type: inquiryFieldTypeSchema,
|
||||
label: z.string().min(1),
|
||||
required: z.boolean().optional(),
|
||||
options: z.array(inquiryFieldOptionSchema).optional(),
|
||||
})
|
||||
.passthrough();
|
||||
.passthrough()
|
||||
.superRefine((field, context) => {
|
||||
if (["select", "radio", "multi-select"].includes(field.type) && !field.options?.length) {
|
||||
context.addIssue({
|
||||
code: z.ZodIssueCode.custom,
|
||||
path: ["options"],
|
||||
message: "选择类字段必须配置 options",
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
export const fieldSnapshotSchema = z.array(inquiryFieldSchema).superRefine((fields, context) => {
|
||||
const seen = new Set<string>();
|
||||
|
||||
Reference in new issue
Block a user