Slack: validate blocks input shape centrally

This commit is contained in:
Colin
2026-02-16 12:30:33 -05:00
committed by Peter Steinberger
parent e023c84d78
commit 10d876e319
6 changed files with 119 additions and 37 deletions

View File

@@ -0,0 +1,41 @@
import { describe, expect, it } from "vitest";
import { parseSlackBlocksInput } from "./blocks-input.js";
describe("parseSlackBlocksInput", () => {
it("returns undefined when blocks are missing", () => {
expect(parseSlackBlocksInput(undefined)).toBeUndefined();
expect(parseSlackBlocksInput(null)).toBeUndefined();
});
it("accepts blocks arrays", () => {
const parsed = parseSlackBlocksInput([{ type: "divider" }]);
expect(parsed).toEqual([{ type: "divider" }]);
});
it("accepts JSON blocks strings", () => {
const parsed = parseSlackBlocksInput(
'[{"type":"section","text":{"type":"mrkdwn","text":"hi"}}]',
);
expect(parsed).toEqual([{ type: "section", text: { type: "mrkdwn", text: "hi" } }]);
});
it("rejects invalid JSON", () => {
expect(() => parseSlackBlocksInput("{bad-json")).toThrow(/valid JSON/i);
});
it("rejects non-array payloads", () => {
expect(() => parseSlackBlocksInput({ type: "divider" })).toThrow(/must be an array/i);
});
it("rejects empty arrays", () => {
expect(() => parseSlackBlocksInput([])).toThrow(/at least one block/i);
});
it("rejects non-object blocks", () => {
expect(() => parseSlackBlocksInput(["not-a-block"])).toThrow(/must be an object/i);
});
it("rejects blocks without type", () => {
expect(() => parseSlackBlocksInput([{}])).toThrow(/non-empty string type/i);
});
});

41
src/slack/blocks-input.ts Normal file
View File

@@ -0,0 +1,41 @@
import type { Block, KnownBlock } from "@slack/web-api";
const SLACK_MAX_BLOCKS = 50;
function parseBlocksJson(raw: string) {
try {
return JSON.parse(raw);
} catch {
throw new Error("blocks must be valid JSON");
}
}
function assertBlocksArray(raw: unknown) {
if (!Array.isArray(raw)) {
throw new Error("blocks must be an array");
}
if (raw.length === 0) {
throw new Error("blocks must contain at least one block");
}
if (raw.length > SLACK_MAX_BLOCKS) {
throw new Error(`blocks cannot exceed ${SLACK_MAX_BLOCKS} items`);
}
for (const block of raw) {
if (!block || typeof block !== "object" || Array.isArray(block)) {
throw new Error("each block must be an object");
}
const type = (block as { type?: unknown }).type;
if (typeof type !== "string" || type.trim().length === 0) {
throw new Error("each block must include a non-empty string type");
}
}
}
export function parseSlackBlocksInput(raw: unknown): (Block | KnownBlock)[] | undefined {
if (raw == null) {
return undefined;
}
const parsed = typeof raw === "string" ? parseBlocksJson(raw) : raw;
assertBlocksArray(parsed);
return parsed as (Block | KnownBlock)[];
}