Skip to content

Commit

Permalink
feat: Expand the DID scalar regex to accommodate did:pkh (#1801)
Browse files Browse the repository at this point in the history
* expand the DID scalar regex to accommodate did:pkh

* Add changest

---------

Co-authored-by: Arda TANRIKULU <ardatanrikulu@gmail.com>
  • Loading branch information
mcclurejt and ardatan committed Mar 24, 2023
1 parent 9258088 commit 7337d43
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/curly-pugs-shave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'graphql-scalars': patch
---

Expand the DID scalar regex to accommodate did:pkh
3 changes: 2 additions & 1 deletion src/scalars/DID.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import { GraphQLScalarType, Kind, GraphQLScalarTypeConfig, ASTNode } from 'graph
import { createGraphQLError } from '../error.js';

// See: https://www.w3.org/TR/2021/PR-did-core-20210803/#did-syntax
const DID_REGEX = /^did:[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+:[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+$/;
const DID_REGEX =
/^did:[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+:[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]*:?[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]*:?[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]*$/;

const validate = (value: any, ast?: ASTNode) => {
if (typeof value !== 'string') {
Expand Down
25 changes: 24 additions & 1 deletion tests/DID.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Kind } from 'graphql/language';
import { GraphQLDID } from '../src/scalars/DID.js';

describe('DID', () => {
describe('valid - DID', () => {
describe('valid - DID example', () => {
test('serialize', () => {
expect(GraphQLDID.serialize('did:example:123456789abcdefghi')).toBe('did:example:123456789abcdefghi');
});
Expand All @@ -19,6 +19,29 @@ describe('DID', () => {
});
});

describe('valid - DID pkh', () => {
test('serialize', () => {
expect(GraphQLDID.serialize('did:pkh:eip155:1:0x6317FBFf7a0A016eD485fc33f4Ae55F91C403377')).toBe(
'did:pkh:eip155:1:0x6317FBFf7a0A016eD485fc33f4Ae55F91C403377'
);
});

test('parseValue', () => {
expect(GraphQLDID.parseValue('did:pkh:eip155:1:0x6317FBFf7a0A016eD485fc33f4Ae55F91C403377')).toEqual(
'did:pkh:eip155:1:0x6317FBFf7a0A016eD485fc33f4Ae55F91C403377'
);
});

test('parseLiteral', () => {
expect(
GraphQLDID.parseLiteral(
{ value: 'did:pkh:eip155:1:0x6317FBFf7a0A016eD485fc33f4Ae55F91C403377', kind: Kind.STRING },
{}
)
).toEqual('did:pkh:eip155:1:0x6317FBFf7a0A016eD485fc33f4Ae55F91C403377');
});
});

describe('invalid', () => {
describe('not a DID', () => {
expect(() => GraphQLDID.serialize('invaliddid')).toThrow(/Value is not a valid DID/);
Expand Down

0 comments on commit 7337d43

Please sign in to comment.