You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

38 lines
1.1 KiB

const MAX_NAME = 32;
const MAX_BODY = 500;
export class GuestCommentValidationError extends Error {
constructor(message: string) {
super(message);
this.name = "GuestCommentValidationError";
}
}
export function normalizeGuestDisplayName(raw: string): string {
const t = raw.trim();
if (!t) {
throw new GuestCommentValidationError("请填写昵称");
}
if (t.length > MAX_NAME) {
throw new GuestCommentValidationError("昵称过长");
}
return t;
}
/** 返回 trim 后正文;违规抛 GuestCommentValidationError(由 API 映射为 400) */
export function validateGuestCommentBody(raw: string): string {
const t = raw.trim();
if (!t) {
throw new GuestCommentValidationError("请填写内容");
}
if (t.length > MAX_BODY) {
throw new GuestCommentValidationError("内容过长");
}
if (/https?:\/\//i.test(t) || /\bwww\./i.test(t)) {
throw new GuestCommentValidationError("不能包含链接");
}
if (/\[[^\]]*\]\([^)]*\)/.test(t)) {
throw new GuestCommentValidationError("不能使用 Markdown 链接");
}
return t;
}