Skip to content

Commit df84d85

Browse files
committed
fix(validation): tighten isValidDid regex and document breaking behaviour
- Regex now rejects trailing colons in method-specific-id (e.g. did:x:abc:) per W3C DID Core 1.0 spec: id must end with at least one non-colon idchar - Replace duplicate test case (did:plc:) with trailing-colon case (did:example:abc:) - Changeset notes the potentially breaking constructor behaviour
1 parent 6f914e5 commit df84d85

3 files changed

Lines changed: 8 additions & 3 deletions

File tree

.changeset/did-format-validation.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,7 @@ Add `isValidDid()` utility function for DID format validation
77
- Validates DID format (did:method:identifier) with support for numeric method names per W3C spec
88
- Exported from `@hypercerts-org/sdk-core` for consumer use
99
- `BlobOperationsImpl` constructor now validates `repoDid` and throws `ValidationError` for invalid formats
10+
11+
> **⚠️ Potentially breaking:** callers that previously passed invalid DID strings to `BlobOperationsImpl` (directly or
12+
> via `Repository`) will now receive a `ValidationError` at construction time instead of silently accepting the value.
13+
> Use `isValidDid(repoDid)` to check before constructing if needed.

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ export function isValidDid(did: string): boolean {
4646
// DID format: did:<method>:<method-specific-id>
4747
// Method: lowercase letters and digits (per W3C DID Core spec)
4848
// Identifier: alphanumeric plus . _ : % -
49-
return /^did:[a-z0-9]+:[a-zA-Z0-9._:%-]+$/.test(did);
49+
// method-specific-id must end with at least one non-colon idchar (W3C DID Core 1.0)
50+
return /^did:[a-z0-9]+:(?:[a-zA-Z0-9._%-]+:)*[a-zA-Z0-9._%-]+$/.test(did);
5051
}
5152

5253
/**

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ describe("isValidDid", () => {
6161
expect(isValidDid("did:plc:")).toBe(false);
6262
});
6363

64-
it("should reject did:method: with empty identifier", () => {
65-
expect(isValidDid("did:plc:")).toBe(false);
64+
it("should reject DID with trailing colon in identifier", () => {
65+
expect(isValidDid("did:example:abc:")).toBe(false);
6666
});
6767

6868
it("should reject method with uppercase letters", () => {

0 commit comments

Comments
 (0)