Skip to content

Commit

Permalink
Add @ucanto/validator DID.from (#191)
Browse files Browse the repository at this point in the history
  • Loading branch information
gobengo committed Jan 5, 2023
1 parent a872395 commit 582c4c5
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
6 changes: 6 additions & 0 deletions packages/validator/src/schema/did.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,9 @@ export const match = options =>
/** @type {Schema.Schema<API.DID<Method> & API.URI<"did:">>} */ (
Schema.string().refine(new DIDSchema(options.method))
)

/**
* Create a DID string from any input (or throw)
* @param {unknown} input
*/
export const from = input => match({}).from(input)
38 changes: 38 additions & 0 deletions packages/validator/test/extra-schema.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -381,3 +381,41 @@ test('URI.from', () => {
})
}
}

{
/** @type {Array<[unknown, null|{ name: string, message: string }]>} */
const dataset = [
['did:key:foo', null],
['did:web:example.com', null],
['did:twosegments', null],
[
'notdid',
{
name: 'SchemaError',
message: 'Expected a did: but got "notdid" instead',
},
],
[
undefined,
{
name: 'TypeError',
message: 'Expected value of type string instead got undefined',
},
],
]
for (const [did, errorExpectation] of dataset) {
test(`DID.from("${did}")`, () => {
let error
try {
DID.from(did)
} catch (_error) {
error = _error
}
if (errorExpectation) {
assert.containSubset(error, errorExpectation)
} else {
assert.notOk(error, 'expected no error, but got an error')
}
})
}
}

0 comments on commit 582c4c5

Please sign in to comment.