diff options
| author | Mostafa Wahied <97263286+Mostafa-Wahied@users.noreply.github.com> | 2025-06-22 11:47:51 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-06-22 19:47:51 +0100 |
| commit | 112aa9d942ef0f8548c3728e6218c27cc335a601 (patch) | |
| tree | 9b0c545fb5b16604e9061e59ebd3d57d7033b4a7 /packages/trpc | |
| parent | c70d64d4cde2bf2acc5c4164eef79c40fd58aa42 (diff) | |
| download | karakeep-112aa9d942ef0f8548c3728e6218c27cc335a601.tar.zst | |
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 <me@mbassem.com>
Diffstat (limited to 'packages/trpc')
| -rw-r--r-- | packages/trpc/routers/bookmarks.ts | 9 | ||||
| -rw-r--r-- | packages/trpc/routers/tags.test.ts | 34 |
2 files changed, 40 insertions, 3 deletions
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<CustomTestContext>("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<CustomTestContext>("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"); + }); }); |
