Skip to content

Commit

Permalink
fix(docmaps-sdk): add @context when missing (#54)
Browse files Browse the repository at this point in the history
  • Loading branch information
ships authored May 11, 2023
1 parent 6c67dc6 commit d003311
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 11 deletions.
40 changes: 29 additions & 11 deletions packages/ts-sdk/src/test/types.test.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
import test, { ExecutionContext } from 'ava'
import { PartialExamples as ex } from './__fixtures__'
import * as dm from '../types'
import { Either, isRight } from 'fp-ts/lib/Either'
import util from 'util'
import * as E from 'fp-ts/lib/Either'
import * as util from 'util'
import { pipe } from 'fp-ts/lib/pipeable'

function isRightArray(t: ExecutionContext, arr: Either<unknown, any>[], len: number) {
function isRightArray<T>(
t: ExecutionContext,
arr: E.Either<unknown, T>[],
len: number,
proc?: (_a: readonly T[]) => void,
) {
t.is(arr.length, len)

arr.map((a) => {
if (isRight(a)) {
t.pass()
} else {
t.fail(`Error parsing: ${util.inspect(a.left, { depth: 18 })}`)
}
})
pipe(
arr,
E.sequenceArray,
E.mapLeft((e) => t.fail(`Error parsing: ${util.inspect(e, { depth: 18 })}`)),
//eslint-disable-next-line
E.map(proc || (() => {})),
)
}

test('Codec parsing DocmapOnlineAccount', (t) => {
Expand Down Expand Up @@ -83,5 +89,17 @@ test('Codec parsing Docmap', (t) => {
const v = ex.elife.Docmap.flatMap((x) => {
return dm.Docmap.decode(x)
})
isRightArray(t, v, 2)
isRightArray(t, v, 2, (arr) => {
t.deepEqual(arr[0]?.['@context'], 'https://w3id.org/docmaps/context.jsonld')
})
})

test('Codec inserts missing @context key for Docmap', (t) => {
const v = ex.elife.Docmap.flatMap((x) => {
const { ['@context']: _, ...stripped } = x
return dm.Docmap.decode(stripped)
})
isRightArray(t, v, 2, (arr) => {
t.deepEqual(arr[0]?.['@context'], 'https://w3id.org/docmaps/context.jsonld')
})
})
6 changes: 6 additions & 0 deletions packages/ts-sdk/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as t from 'io-ts'
import { fromNullable } from 'io-ts-types/lib/fromNullable'
import { UrlFromString, DateFromUnknown } from './util'

function arrayOrOneOf(literalStrings: string[]) {
Expand Down Expand Up @@ -137,6 +138,11 @@ export const DocmapStep = t.intersection([
export const Docmap = t.intersection([
t.type({
id: t.string,
// only one legal value, and fill it if absent
'@context': fromNullable(
t.literal('https://w3id.org/docmaps/context.jsonld'),
'https://w3id.org/docmaps/context.jsonld',
),
type: arrayOrOneOf([
// TODO support something where docmaps: is prefixed
// t.literal('docmaps:docmap'),
Expand Down

0 comments on commit d003311

Please sign in to comment.