fix: unify panic room code format and validate room join id
This commit is contained in:
@@ -7,6 +7,7 @@ import {
|
||||
validateIdeaContent,
|
||||
validateRoomName,
|
||||
requireString,
|
||||
validatePanicRoomId,
|
||||
} from "@/lib/validation";
|
||||
|
||||
describe("validateUsername", () => {
|
||||
@@ -125,3 +126,16 @@ describe("requireString", () => {
|
||||
expect(() => requireString(123, "字段")).toThrow(ApiError);
|
||||
});
|
||||
});
|
||||
|
||||
describe("validatePanicRoomId", () => {
|
||||
it("accepts 6-char alphanumeric room id and normalizes uppercase", () => {
|
||||
expect(validatePanicRoomId("ab12cd")).toBe("AB12CD");
|
||||
expect(validatePanicRoomId("ROOM01")).toBe("ROOM01");
|
||||
});
|
||||
|
||||
it("rejects invalid room id format", () => {
|
||||
expect(() => validatePanicRoomId("1234")).toThrow(ApiError);
|
||||
expect(() => validatePanicRoomId("1234567")).toThrow(ApiError);
|
||||
expect(() => validatePanicRoomId("12-45a")).toThrow(ApiError);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -48,3 +48,16 @@ export function requireString(value: unknown, fieldName: string): string {
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
const PANIC_ROOM_ID_REGEX = /^[A-Z0-9]{6}$/;
|
||||
|
||||
export function validatePanicRoomId(raw: unknown): string {
|
||||
if (!raw || typeof raw !== "string") {
|
||||
throw new ApiError("roomId 不能为空");
|
||||
}
|
||||
const roomId = raw.trim().toUpperCase();
|
||||
if (!PANIC_ROOM_ID_REGEX.test(roomId)) {
|
||||
throw new ApiError("房间号格式无效,应为 6 位字母数字", 400);
|
||||
}
|
||||
return roomId;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user