fix: 强化工作空间字段运行时校验
This commit is contained in:
1 parent
1503f042e0
commit
1a4e806ca8
2 files changed
+33
-10
No files matched your search
@@ -1,4 +1,15 @@
|
|||||||
import { IsArray, IsBoolean, IsIn, IsInt, IsOptional, IsString, Max, Min } from "class-validator";
|
import {
|
||||||
|
IsArray,
|
||||||
|
IsBoolean,
|
||||||
|
IsIn,
|
||||||
|
IsInt,
|
||||||
|
IsObject,
|
||||||
|
IsOptional,
|
||||||
|
IsString,
|
||||||
|
Matches,
|
||||||
|
Max,
|
||||||
|
Min,
|
||||||
|
} from "class-validator";
|
||||||
|
|
||||||
export class UpdateBrandDto {
|
export class UpdateBrandDto {
|
||||||
@IsOptional() @IsString() name?: string;
|
@IsOptional() @IsString() name?: string;
|
||||||
@@ -137,21 +148,21 @@ export class UpdateModuleDto {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export class CreateFieldDefinitionDto {
|
export class CreateFieldDefinitionDto {
|
||||||
@IsString() scope!: string;
|
@IsIn(["profile", "service", "inquiry"]) scope!: string;
|
||||||
@IsString() key!: string;
|
@IsString() @Matches(/^[a-z][a-z0-9_]*$/) key!: string;
|
||||||
@IsString() label!: string;
|
@IsString() label!: string;
|
||||||
@IsIn(["text", "textarea", "number", "date", "select", "multi-select", "image", "url", "phone"])
|
@IsIn(["text", "textarea", "number", "date", "select", "multi-select", "image", "url", "phone"])
|
||||||
type!: string;
|
type!: string;
|
||||||
@IsOptional() @IsBoolean() required?: boolean;
|
@IsOptional() @IsBoolean() required?: boolean;
|
||||||
@IsOptional() @IsBoolean() isVisible?: boolean;
|
@IsOptional() @IsBoolean() isVisible?: boolean;
|
||||||
@IsOptional() @IsInt() @Min(0) @Max(9999) sortOrder?: number;
|
@IsOptional() @IsInt() @Min(0) @Max(9999) sortOrder?: number;
|
||||||
@IsOptional() options?: unknown;
|
@IsOptional() @IsArray() options?: unknown[];
|
||||||
@IsOptional() visibleWhen?: unknown;
|
@IsOptional() @IsObject() visibleWhen?: Record<string, unknown>;
|
||||||
}
|
}
|
||||||
|
|
||||||
export class UpdateFieldDefinitionDto {
|
export class UpdateFieldDefinitionDto {
|
||||||
@IsOptional() @IsString() scope?: string;
|
@IsOptional() @IsIn(["profile", "service", "inquiry"]) scope?: string;
|
||||||
@IsOptional() @IsString() key?: string;
|
@IsOptional() @IsString() @Matches(/^[a-z][a-z0-9_]*$/) key?: string;
|
||||||
@IsOptional() @IsString() label?: string;
|
@IsOptional() @IsString() label?: string;
|
||||||
@IsOptional()
|
@IsOptional()
|
||||||
@IsIn(["text", "textarea", "number", "date", "select", "multi-select", "image", "url", "phone"])
|
@IsIn(["text", "textarea", "number", "date", "select", "multi-select", "image", "url", "phone"])
|
||||||
@@ -159,8 +170,8 @@ export class UpdateFieldDefinitionDto {
|
|||||||
@IsOptional() @IsBoolean() required?: boolean;
|
@IsOptional() @IsBoolean() required?: boolean;
|
||||||
@IsOptional() @IsBoolean() isVisible?: boolean;
|
@IsOptional() @IsBoolean() isVisible?: boolean;
|
||||||
@IsOptional() @IsInt() @Min(0) @Max(9999) sortOrder?: number;
|
@IsOptional() @IsInt() @Min(0) @Max(9999) sortOrder?: number;
|
||||||
@IsOptional() options?: unknown;
|
@IsOptional() @IsArray() options?: unknown[];
|
||||||
@IsOptional() visibleWhen?: unknown;
|
@IsOptional() @IsObject() visibleWhen?: Record<string, unknown>;
|
||||||
}
|
}
|
||||||
|
|
||||||
export class CreateInquiryFormDto {
|
export class CreateInquiryFormDto {
|
||||||
|
|||||||
@@ -47,6 +47,9 @@ test("health and public content are available without authentication", async ()
|
|||||||
});
|
});
|
||||||
|
|
||||||
test("admin can install a template and public workspace exposes enabled configuration", async () => {
|
test("admin can install a template and public workspace exposes enabled configuration", async () => {
|
||||||
|
await prisma.brandFieldDefinition.deleteMany({ where: { brandId } });
|
||||||
|
await prisma.brandModule.deleteMany({ where: { brandId } });
|
||||||
|
await prisma.brandTemplateInstallation.deleteMany({ where: { brandId } });
|
||||||
const login = await request("/admin/session/login", {
|
const login = await request("/admin/session/login", {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
payload: { username: "admin", password: process.env.SEED_ADMIN_PASSWORD },
|
payload: { username: "admin", password: process.env.SEED_ADMIN_PASSWORD },
|
||||||
@@ -85,6 +88,9 @@ test("admin can install a template and public workspace exposes enabled configur
|
|||||||
});
|
});
|
||||||
|
|
||||||
test("admin can customize workspace modules and fields with brand isolation", async () => {
|
test("admin can customize workspace modules and fields with brand isolation", async () => {
|
||||||
|
await prisma.brandFieldDefinition.deleteMany({ where: { brandId } });
|
||||||
|
await prisma.brandModule.deleteMany({ where: { brandId } });
|
||||||
|
await prisma.brandTemplateInstallation.deleteMany({ where: { brandId } });
|
||||||
const login = await request("/admin/session/login", {
|
const login = await request("/admin/session/login", {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
payload: { username: "admin", password: process.env.SEED_ADMIN_PASSWORD },
|
payload: { username: "admin", password: process.env.SEED_ADMIN_PASSWORD },
|
||||||
@@ -100,6 +106,12 @@ test("admin can customize workspace modules and fields with brand isolation", as
|
|||||||
headers: { authorization: `Bearer ${token}` },
|
headers: { authorization: `Bearer ${token}` },
|
||||||
});
|
});
|
||||||
const moduleId = JSON.parse(workspace.body).modules[0].id;
|
const moduleId = JSON.parse(workspace.body).modules[0].id;
|
||||||
|
const invalidField = await request("/admin/workspace/fields", {
|
||||||
|
method: "POST",
|
||||||
|
headers: { authorization: `Bearer ${token}` },
|
||||||
|
payload: { scope: "unknown", key: "Bad-Key", label: "非法字段", type: "text" },
|
||||||
|
});
|
||||||
|
assert.equal(invalidField.statusCode, 400);
|
||||||
const updatedModule = await request(`/admin/workspace/modules/${moduleId}`, {
|
const updatedModule = await request(`/admin/workspace/modules/${moduleId}`, {
|
||||||
method: "PATCH",
|
method: "PATCH",
|
||||||
headers: { authorization: `Bearer ${token}` },
|
headers: { authorization: `Bearer ${token}` },
|
||||||
@@ -111,7 +123,7 @@ test("admin can customize workspace modules and fields with brand isolation", as
|
|||||||
headers: { authorization: `Bearer ${token}` },
|
headers: { authorization: `Bearer ${token}` },
|
||||||
payload: {
|
payload: {
|
||||||
scope: "profile",
|
scope: "profile",
|
||||||
key: `region-${Date.now()}`,
|
key: `region_${Date.now()}`,
|
||||||
label: "服务区域",
|
label: "服务区域",
|
||||||
type: "text",
|
type: "text",
|
||||||
required: false,
|
required: false,
|
||||||
|
|||||||
Reference in new issue
Block a user