Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/metadata/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"@grants-stack-indexer/repository": "workspace:*",
"@grants-stack-indexer/shared": "workspace:*",
"axios": "1.7.7",
"multiformats": "13.3.2",
"pinata": "1.10.1",
"ts-retry": "5.0.1",
"zod": "3.23.8"
Expand Down
25 changes: 22 additions & 3 deletions packages/metadata/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,23 @@
export const isValidCid = (cid: string): boolean => {
const cidRegex = /^(Qm[1-9A-HJ-NP-Za-km-z]{44}|baf[0-9A-Za-z]{50,})$/;
return cidRegex.test(cid);
import { CID } from "multiformats/cid";

export const isValidCid = (input: string): boolean => {
try {
// Extract CID part from optional paths/params
const match = input.match(/^([^/?#]+)(?:[/?#].*)?$/);
if (!match) return false;

const cid = match[1];

if (!cid) return false;

// Quick regex check for basic format
const cidRegex = /^(Qm[1-9A-HJ-NP-Za-km-z]{44}|baf[0-9A-Za-z]+)$/;
if (!cidRegex.test(cid)) return false;

// Full validation using multiformats/cid
CID.parse(cid);
return true;
} catch {
return false;
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,9 @@ describe("isValidCid", () => {
expect(isValidCid(input as unknown as string)).toBe(false);
});
});

it("returns true when CID has path or query parameters", () => {
const cidWithParams = "QmW2WQi7j6c7UgJTarActp7tDNikE4B2qXtFCfLPdsgaTQ/path?param=value";
expect(isValidCid(cidWithParams)).toBe(true);
});
});
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading