Skip to content

Commit 6f914e5

Browse files
committed
fix(sdk-core): update DID regex to allow digits in method names per W3C spec
- Update DID validation regex to allow digits in method names per W3C spec - Add test cases for DIDs with numeric method names (did:key2:, did:btc1:) - Update JSDoc to reflect digits allowed in DID method names - Add package-lock.json to .gitignore to prevent future commits - Add changeset for DID format validation feature
1 parent 06befd9 commit 6f914e5

4 files changed

Lines changed: 25 additions & 7 deletions

File tree

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
"@hypercerts-org/sdk-core": minor
3+
---
4+
5+
Add `isValidDid()` utility function for DID format validation
6+
7+
- Validates DID format (did:method:identifier) with support for numeric method names per W3C spec
8+
- Exported from `@hypercerts-org/sdk-core` for consumer use
9+
- `BlobOperationsImpl` constructor now validates `repoDid` and throws `ValidationError` for invalid formats

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ coverage.json
1818
npm-debug.log*
1919
yarn-debug.log*
2020
yarn-error.log*
21+
package-lock.json
2122
.DS_Store
2223
/.idea
2324
stats.html

packages/sdk-core/src/core/types.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export type DID = string;
2626
* Validates that a string is a valid DID format.
2727
*
2828
* DIDs must follow the format: `did:<method>:<method-specific-id>`
29-
* where method is lowercase letters and the identifier contains
29+
* where method is lowercase letters and digits, and the identifier contains
3030
* alphanumeric characters plus `.`, `_`, `:`, `%`, and `-`.
3131
*
3232
* @param did - The string to validate
@@ -44,9 +44,9 @@ export type DID = string;
4444
*/
4545
export function isValidDid(did: string): boolean {
4646
// DID format: did:<method>:<method-specific-id>
47-
// Method: lowercase letters only
47+
// Method: lowercase letters and digits (per W3C DID Core spec)
4848
// Identifier: alphanumeric plus . _ : % -
49-
return /^did:[a-z]+:[a-zA-Z0-9._:%-]+$/.test(did);
49+
return /^did:[a-z0-9]+:[a-zA-Z0-9._:%-]+$/.test(did);
5050
}
5151

5252
/**

packages/sdk-core/tests/core/types.test.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,18 @@ describe("isValidDid", () => {
3030
it("should accept DID with hyphens and underscores", () => {
3131
expect(isValidDid("did:example:my-test_id")).toBe(true);
3232
});
33+
34+
it("should accept DID with method containing digits", () => {
35+
expect(isValidDid("did:key2:abc123")).toBe(true);
36+
});
37+
38+
it("should accept DID with method containing multiple digits", () => {
39+
expect(isValidDid("did:btc1:xyz789")).toBe(true);
40+
});
41+
42+
it("should accept DID with method that is all digits", () => {
43+
expect(isValidDid("did:123:identifier")).toBe(true);
44+
});
3345
});
3446

3547
describe("invalid DIDs", () => {
@@ -57,10 +69,6 @@ describe("isValidDid", () => {
5769
expect(isValidDid("did:PLC:abc123")).toBe(false);
5870
});
5971

60-
it("should reject method with numbers", () => {
61-
expect(isValidDid("did:plc2:abc123")).toBe(false);
62-
});
63-
6472
it("should reject random URL", () => {
6573
expect(isValidDid("https://example.com")).toBe(false);
6674
});

0 commit comments

Comments
 (0)