43 lines
1.3 KiB
TypeScript
43 lines
1.3 KiB
TypeScript
/**
|
|
* Checks the comment request bodies, for the comment routes to answer a 400.
|
|
*
|
|
* @module
|
|
*/
|
|
|
|
import type {
|
|
CreateCommentRequest,
|
|
UpdateCommentRequest,
|
|
} from "../../shared/comment.ts";
|
|
import { VALIDATION } from "../../shared/validation.ts";
|
|
|
|
/**
|
|
* Checks whether `obj` is a {@link CreateCommentRequest} whose body isn't
|
|
* blank and fits `VALIDATION.COMMENT_BODY_MAX`. A null `parentId` passes, as a
|
|
* top-level comment.
|
|
*/
|
|
export function isCreateCommentRequest(
|
|
obj: unknown,
|
|
): obj is CreateCommentRequest {
|
|
if (!obj || typeof obj !== "object") return false;
|
|
const o = obj as Record<string, unknown>;
|
|
return typeof o.body === "string" &&
|
|
(o.body as string).trim().length > 0 &&
|
|
(o.body as string).length <= VALIDATION.COMMENT_BODY_MAX &&
|
|
(!("parentId" in o) || typeof o.parentId === "string" ||
|
|
o.parentId === null);
|
|
}
|
|
|
|
/**
|
|
* Checks whether `obj` is an {@link UpdateCommentRequest} whose body isn't
|
|
* blank and fits `VALIDATION.COMMENT_BODY_MAX`.
|
|
*/
|
|
export function isUpdateCommentRequest(
|
|
obj: unknown,
|
|
): obj is UpdateCommentRequest {
|
|
if (!obj || typeof obj !== "object") return false;
|
|
const o = obj as Record<string, unknown>;
|
|
return typeof o.body === "string" &&
|
|
(o.body as string).trim().length > 0 &&
|
|
(o.body as string).length <= VALIDATION.COMMENT_BODY_MAX;
|
|
}
|