/** * Guards for the dump request bodies. File dumps arrive as multipart forms * and are checked in their route. * * @module */ import type { CreateUrlDumpRequest, UpdateDumpRequest, } from "../../shared/dump.ts"; import { VALIDATION } from "../../shared/validation.ts"; function isStringArray(value: unknown): value is string[] { return Array.isArray(value) && value.every((v) => typeof v === "string"); } /** * Checks whether `obj` is a valid {@link CreateUrlDumpRequest}, its title and * comment within the `VALIDATION` limits. */ export function isCreateUrlDumpRequest( obj: unknown, ): obj is CreateUrlDumpRequest { if ( !obj || typeof obj !== "object" || !("url" in obj) || typeof obj.url !== "string" ) return false; const o = obj as Record; if ( "comment" in o && typeof o.comment !== "string" && o.comment !== null ) return false; if ( typeof o.comment === "string" && (o.comment as string).length > VALIDATION.DUMP_COMMENT_MAX ) return false; if ("title" in o && typeof o.title !== "string" && o.title !== null) { return false; } if ( typeof o.title === "string" && (o.title as string).length > VALIDATION.DUMP_TITLE_MAX ) return false; if ("isPrivate" in o && typeof o.isPrivate !== "boolean") return false; if ("categoryIds" in o && !isStringArray(o.categoryIds)) return false; return true; } /** * Checks whether `obj` is a valid {@link UpdateDumpRequest}: a title, when * given, that isn't blank, and title and comment within the `VALIDATION` * limits. */ export function isUpdateDumpRequest(obj: unknown): obj is UpdateDumpRequest { if (!obj || typeof obj !== "object") return false; const o = obj as Record; if ("url" in o && typeof o.url !== "string" && o.url !== null) return false; if ("title" in o) { if (typeof o.title !== "string") return false; const trimmed = (o.title as string).trim(); if (!trimmed || trimmed.length > VALIDATION.DUMP_TITLE_MAX) return false; } if ( "comment" in o && typeof o.comment !== "string" && o.comment !== null ) return false; if ( typeof o.comment === "string" && (o.comment as string).length > VALIDATION.DUMP_COMMENT_MAX ) return false; if ("isPrivate" in o && typeof o.isPrivate !== "boolean") return false; if ("categoryIds" in o && !isStringArray(o.categoryIds)) return false; return true; }