From 112aa9d942ef0f8548c3728e6218c27cc335a601 Mon Sep 17 00:00:00 2001 From: Mostafa Wahied <97263286+Mostafa-Wahied@users.noreply.github.com> Date: Sun, 22 Jun 2025 11:47:51 -0700 Subject: fix(tags): normalise leading hashes in tag names (#1317) (#1351) * fix(tags): normalise leading hashes in tag names (#1317) * move the transformation to zod * fix openapi spec --------- Co-authored-by: Mohamed Bassem --- packages/trpc/routers/bookmarks.ts | 9 ++++++--- packages/trpc/routers/tags.test.ts | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 3 deletions(-) (limited to 'packages/trpc') diff --git a/packages/trpc/routers/bookmarks.ts b/packages/trpc/routers/bookmarks.ts index 815cf90d..2a02a0cd 100644 --- a/packages/trpc/routers/bookmarks.ts +++ b/packages/trpc/routers/bookmarks.ts @@ -50,6 +50,7 @@ import { zSearchBookmarksRequestSchema, zUpdateBookmarksRequestSchema, } from "@karakeep/shared/types/bookmarks"; +import { normalizeTagName } from "@karakeep/shared/utils/tag"; import type { AuthedContext, Context } from "../index"; import { authedProcedure, router } from "../index"; @@ -867,9 +868,11 @@ export const bookmarksAppRouter = router({ }; } - const toAddTagNames = input.attach.flatMap((i) => - i.tagName ? [i.tagName] : [], - ); + const toAddTagNames = input.attach + .flatMap((i) => (i.tagName ? [i.tagName] : [])) + .map(normalizeTagName) // strip leading # + .filter((n) => n.length > 0); // drop empty results + const toAddTagIds = input.attach.flatMap((i) => i.tagId ? [i.tagId] : [], ); diff --git a/packages/trpc/routers/tags.test.ts b/packages/trpc/routers/tags.test.ts index c5f92cb2..1e7118d2 100644 --- a/packages/trpc/routers/tags.test.ts +++ b/packages/trpc/routers/tags.test.ts @@ -143,4 +143,38 @@ describe("Tags Routes", () => { const resUser2 = await apiUser2.list(); expect(resUser2.tags.some((tag) => tag.name === "user1Tag")).toBeFalsy(); // Should not see other user's tags }); + + test("create strips extra leading hashes", async ({ + apiCallers, + db, + }) => { + const api = apiCallers[0].tags; + + const created = await api.create({ name: "##demo" }); + expect(created.name).toBe("demo"); + + // Confirm DB row too + const row = await db.query.bookmarkTags.findFirst({ + where: eq(bookmarkTags.id, created.id), + }); + expect(row?.name).toBe("demo"); + }); + + test("update normalizes leading hashes", async ({ + apiCallers, + db, + }) => { + const api = apiCallers[0].tags; + + const created = await api.create({ name: "#foo" }); + const updated = await api.update({ tagId: created.id, name: "##bar" }); + + expect(updated.name).toBe("bar"); + + // Confirm DB row too + const row = await db.query.bookmarkTags.findFirst({ + where: eq(bookmarkTags.id, updated.id), + }); + expect(row?.name).toBe("bar"); + }); }); -- cgit v1.2.3-70-g09d2