/** * Guards for the comment request bodies. * * @module */ import type { CreateCommentRequest, UpdateCommentRequest, } from "../../shared/comment.ts"; import { VALIDATION } from "../../shared/validation.ts"; /** * Checks whether `obj` is a valid {@link CreateCommentRequest}: a body that * isn't blank and fits `VALIDATION.COMMENT_BODY_MAX`. */ export function isCreateCommentRequest( obj: unknown, ): obj is CreateCommentRequest { if (!obj || typeof obj !== "object") return false; const o = obj as Record; 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 a valid {@link UpdateCommentRequest}: a body that * 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; return typeof o.body === "string" && (o.body as string).trim().length > 0 && (o.body as string).length <= VALIDATION.COMMENT_BODY_MAX; }