Validation: the dump validators refuse the nulls their shared types don't allow (a null title or note on creation, a null URL on edit); a playlist title of spaces is refused and titles are stored trimmed; usernames and emails are checked by the same USERNAME_RE and EMAIL_RE in shared/validation.ts on sign-up, on PATCH /api/users/me (which checked other limits) and in the sign-up form. Mentions are read by shared/mention.ts's mentionedUsernames, which skips code and URLs as Markdown shows them: https://mastodon.social/@bob or a `@bob` in code notified bob; the editor's suggestions derive their trigger from the same pattern instead of a copy. Following an unknown user is a 404 rather than a foreign-key 500, and following a private playlist a 404 rather than a 403 that confirmed it exists. `ws` and `emojibase` are reserved category slugs, since the server answers them before the app. GET /api/users/me no longer turns every error into a 500. Dead code goes: adminOnlyMiddleware, verifyEmailTransport, listUsers, deleteUser with disconnectUser and the force_logout message only it sent, PATCH /api/notifications/:id/read (with markOneRead), isSlugAvailable, the unreachable 500 in fetchComment, and the `if (!ctx.state.user)` checks behind authMiddleware. 214 backend and 627 frontend tests pass. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
72 lines
2.5 KiB
TypeScript
72 lines
2.5 KiB
TypeScript
/**
|
|
* Checks the JSON bodies that `routes/dumps.ts` takes to post a link and to
|
|
* edit a dump. A file dump arrives as a multipart form instead, and
|
|
* `dump-service.ts` checks its title and comment.
|
|
*
|
|
* @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 {@link CreateUrlDumpRequest} whose title and
|
|
* comment fit the `VALIDATION` limits. It leaves the URL to `createUrlDump`,
|
|
* which rejects anything but a public http(s) URL.
|
|
*/
|
|
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<string, unknown>;
|
|
if ("comment" in o && typeof o.comment !== "string") 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") 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 an {@link UpdateDumpRequest} whose title and comment
|
|
* fit the `VALIDATION` limits. Unlike on creation, a title that is given must
|
|
* not be blank.
|
|
*/
|
|
export function isUpdateDumpRequest(obj: unknown): obj is UpdateDumpRequest {
|
|
if (!obj || typeof obj !== "object") return false;
|
|
const o = obj as Record<string, unknown>;
|
|
if ("url" in o && typeof o.url !== "string") 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;
|
|
}
|