feat: 增加扩展配置校验器
This commit is contained in:
1 parent
29d14378a7
commit
8baa042255
7 files changed
+85
-32
No files matched your search
@@ -21,6 +21,7 @@
|
||||
"@nestjs/core": "^11.0.11",
|
||||
"@nestjs/platform-fastify": "^11.0.11",
|
||||
"@personal-brand/config": "workspace:^",
|
||||
"@personal-brand/validation": "workspace:^",
|
||||
"@prisma/client": "^6.2.1",
|
||||
"bcryptjs": "^2.4.3",
|
||||
"class-transformer": "^0.5.1",
|
||||
|
||||
@@ -26,6 +26,7 @@ import {
|
||||
UpdateInquiryFormDto,
|
||||
} from "./admin.dto.js";
|
||||
import { TemplateWorkspaceService } from "./template-workspace.service.js";
|
||||
import { validateFieldSnapshot, validateOfferingConfig } from "@personal-brand/validation";
|
||||
|
||||
@Controller("admin/workspace")
|
||||
class AdminWorkspaceController {
|
||||
@@ -62,16 +63,19 @@ class AdminWorkspaceController {
|
||||
): Promise<unknown> {
|
||||
this.requireAdmin(request);
|
||||
const { config, sections, media, cta, ...fields } = body;
|
||||
const configValue = {
|
||||
...(config ?? {}),
|
||||
...(sections === undefined ? {} : { sections }),
|
||||
...(media === undefined ? {} : { media }),
|
||||
...(cta === undefined ? {} : { cta }),
|
||||
};
|
||||
const configIssues = validateOfferingConfig(configValue);
|
||||
if (configIssues.length) throw new BadRequestException("业务提供项配置无效");
|
||||
return this.prisma.offering.create({
|
||||
data: {
|
||||
...fields,
|
||||
brandId: this.brandId(),
|
||||
config: {
|
||||
...(config ?? {}),
|
||||
...(sections === undefined ? {} : { sections }),
|
||||
...(media === undefined ? {} : { media }),
|
||||
...(cta === undefined ? {} : { cta }),
|
||||
} as Prisma.InputJsonValue,
|
||||
config: configValue as Prisma.InputJsonValue,
|
||||
},
|
||||
});
|
||||
}
|
||||
@@ -86,6 +90,14 @@ class AdminWorkspaceController {
|
||||
const item = await this.prisma.offering.findFirst({ where: { id, brandId: this.brandId() } });
|
||||
if (!item) throw new BadRequestException("业务提供项不存在");
|
||||
const { config, sections, media, cta, ...fields } = body;
|
||||
const configValue = {
|
||||
...(config ?? {}),
|
||||
...(sections === undefined ? {} : { sections }),
|
||||
...(media === undefined ? {} : { media }),
|
||||
...(cta === undefined ? {} : { cta }),
|
||||
};
|
||||
if (validateOfferingConfig(configValue).length)
|
||||
throw new BadRequestException("业务提供项配置无效");
|
||||
return this.prisma.offering.update({
|
||||
where: { id },
|
||||
data: {
|
||||
@@ -96,12 +108,7 @@ class AdminWorkspaceController {
|
||||
cta === undefined
|
||||
? {}
|
||||
: {
|
||||
config: {
|
||||
...(config ?? {}),
|
||||
...(sections === undefined ? {} : { sections }),
|
||||
...(media === undefined ? {} : { media }),
|
||||
...(cta === undefined ? {} : { cta }),
|
||||
} as Prisma.InputJsonValue,
|
||||
config: configValue as Prisma.InputJsonValue,
|
||||
}),
|
||||
},
|
||||
});
|
||||
@@ -254,6 +261,8 @@ class AdminWorkspaceController {
|
||||
@Post("forms")
|
||||
createForm(@Req() request: FastifyRequest, @Body() body: CreateInquiryFormDto): Promise<unknown> {
|
||||
this.requireAdmin(request);
|
||||
if (validateFieldSnapshot(body.fieldsSnapshot).length)
|
||||
throw new BadRequestException("咨询表单字段定义无效");
|
||||
return this.prisma.inquiryForm.create({
|
||||
data: {
|
||||
...body,
|
||||
@@ -276,6 +285,8 @@ class AdminWorkspaceController {
|
||||
});
|
||||
if (!item) throw new BadRequestException("咨询表单不存在");
|
||||
const { fieldsSnapshot, ...fields } = body;
|
||||
if (fieldsSnapshot !== undefined && validateFieldSnapshot(fieldsSnapshot).length)
|
||||
throw new BadRequestException("咨询表单字段定义无效");
|
||||
return this.prisma.inquiryForm.update({
|
||||
where: { id },
|
||||
data: {
|
||||
|
||||
@@ -1,20 +0,0 @@
|
||||
export const BASIC_BOOKING_WORKFLOW = {
|
||||
key: "booking.basic",
|
||||
version: "1.0.0",
|
||||
transitions: {
|
||||
received: ["contacting", "cancelled"],
|
||||
contacting: ["confirmed", "cancelled"],
|
||||
confirmed: ["completed", "cancelled"],
|
||||
completed: [],
|
||||
cancelled: [],
|
||||
},
|
||||
} as const;
|
||||
|
||||
export type BasicBookingState = keyof typeof BASIC_BOOKING_WORKFLOW.transitions;
|
||||
|
||||
export function canTransitionBasicBooking(from: string, to: string): boolean {
|
||||
return (
|
||||
from === to ||
|
||||
BASIC_BOOKING_WORKFLOW.transitions[from as BasicBookingState]?.includes(to as never) === true
|
||||
);
|
||||
}
|
||||
@@ -247,6 +247,16 @@ test("admin can manage offerings and public endpoint hides disabled offerings",
|
||||
payload: { username: "admin", password: process.env.SEED_ADMIN_PASSWORD },
|
||||
});
|
||||
const token = JSON.parse(login.body).token;
|
||||
const invalidConfig = await request("/admin/workspace/offerings", {
|
||||
method: "POST",
|
||||
headers: { authorization: `Bearer ${token}` },
|
||||
payload: {
|
||||
kind: "product",
|
||||
name: `无效配置-${Date.now()}`,
|
||||
config: { sections: "必须是数组" },
|
||||
},
|
||||
});
|
||||
assert.equal(invalidConfig.statusCode, 400);
|
||||
const created = await request("/admin/workspace/offerings", {
|
||||
method: "POST",
|
||||
headers: { authorization: `Bearer ${token}` },
|
||||
|
||||
@@ -2,6 +2,12 @@
|
||||
"name": "@personal-brand/validation",
|
||||
"private": true,
|
||||
"type": "module",
|
||||
"exports": {
|
||||
".": {
|
||||
"types": "./src/index.ts",
|
||||
"import": "./src/index.ts"
|
||||
}
|
||||
},
|
||||
"scripts": {
|
||||
"build": "tsc -p tsconfig.json",
|
||||
"lint": "eslint .",
|
||||
|
||||
@@ -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 [];
|
||||
}
|
||||
Generated
+3
@@ -78,6 +78,9 @@ importers:
|
||||
'@personal-brand/config':
|
||||
specifier: workspace:^
|
||||
version: link:../../packages/config
|
||||
'@personal-brand/validation':
|
||||
specifier: workspace:^
|
||||
version: link:../../packages/validation
|
||||
'@prisma/client':
|
||||
specifier: ^6.2.1
|
||||
version: 6.19.3(prisma@6.19.3(typescript@5.9.3))(typescript@5.9.3)
|
||||
|
||||
Reference in new issue
Block a user