feat: 强化动态表单字段校验
This commit is contained in:
1 parent
ef0aa169a6
commit
d6c47ce143
7 files changed
+94
-4
No files matched your search
@@ -0,0 +1,12 @@
|
||||
# 表单配置校验收口
|
||||
|
||||
## 目标
|
||||
|
||||
把动态表单的字段定义变成可靠的 Runtime 合同:类型、key、label、required 和选项在 API 边界统一校验,错误在后台配置时明确反馈,不把坏配置发布到小程序。
|
||||
|
||||
## 验收
|
||||
|
||||
- 字段类型使用 allow-list。
|
||||
- `select`、`radio`、`multi-select` 的 options 结构经过运行时校验。
|
||||
- 重复 key、非法 key、空选项标签或值被拒绝。
|
||||
- API 测试覆盖成功和失败路径,前端构建及小程序 fixture 通过。
|
||||
@@ -0,0 +1,13 @@
|
||||
# 进度
|
||||
|
||||
- 任务:表单配置校验收口
|
||||
- 形态:epic
|
||||
- 进度:3/3
|
||||
- 当前:全部子任务完成
|
||||
- 真相文件:`SUBTASKS.csv`
|
||||
|
||||
## 验证
|
||||
|
||||
- API integration tests:26 passed。
|
||||
- Admin production build:通过。
|
||||
- Miniapp fixture 和 visual fixture:通过。
|
||||
@@ -0,0 +1,4 @@
|
||||
id,title,task_type,depends_on,status,task_dir,acceptance_criteria,validation_command,notes
|
||||
1,字段 schema 强化,single-full,,DONE,tasks/schema,validation package 拒绝非法字段定义,pnpm --filter @personal-brand/api test:integration,已完成
|
||||
2,后台错误反馈,single-full,1,DONE,tasks/schema,配置失败时保留输入并显示可读错误,pnpm --filter @personal-brand/admin build,已完成
|
||||
3,回归验证,single-full,2,DONE,tasks/regression,API 与 miniapp fixture 全通过,pnpm --filter @personal-brand/miniapp test:fixture && pnpm --filter @personal-brand/miniapp test:visual-fixture,已完成
|
||||
|
@@ -0,0 +1,8 @@
|
||||
# 回归验证
|
||||
|
||||
状态:DONE
|
||||
|
||||
- API 集成测试:26 passed。
|
||||
- Admin production build:通过。
|
||||
- Miniapp fixture:通过。
|
||||
- Miniapp visual fixture:通过。
|
||||
@@ -0,0 +1,7 @@
|
||||
# 字段 schema 强化
|
||||
|
||||
状态:DONE
|
||||
|
||||
- 字段 key、type、label、options 统一运行时校验。
|
||||
- 选择类字段必须配置非空 options。
|
||||
- 非法定义在 API 边界返回 400。
|
||||
@@ -486,7 +486,7 @@ test("admin can create and version an inquiry form", async () => {
|
||||
fieldsSnapshot: [
|
||||
{ key: "budget", type: "number", label: "预算", required: true },
|
||||
{
|
||||
key: "contactMethod",
|
||||
key: "contact_method",
|
||||
type: "select",
|
||||
label: "联系渠道",
|
||||
options: [{ label: "微信", value: "wechat" }],
|
||||
@@ -615,6 +615,23 @@ test("admin can create and version an inquiry form", async () => {
|
||||
}
|
||||
});
|
||||
|
||||
test("admin rejects invalid dynamic inquiry field definitions", async () => {
|
||||
const login = await request("/admin/session/login", {
|
||||
method: "POST",
|
||||
payload: { username: "admin", password: process.env.SEED_ADMIN_PASSWORD },
|
||||
});
|
||||
const token = JSON.parse(login.body).token;
|
||||
const invalid = await request("/admin/workspace/forms", {
|
||||
method: "POST",
|
||||
headers: { authorization: `Bearer ${token}` },
|
||||
payload: {
|
||||
name: "非法字段表单",
|
||||
fieldsSnapshot: [{ key: "ContactMethod", type: "select", label: "渠道", options: [] }],
|
||||
},
|
||||
});
|
||||
assert.equal(invalid.statusCode, 400);
|
||||
});
|
||||
|
||||
test("admin can manage offerings and public endpoint hides disabled offerings", async () => {
|
||||
const login = await request("/admin/session/login", {
|
||||
method: "POST",
|
||||
|
||||
@@ -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