From d0033117755e6697548d7dffb9d4c48ead30f59a Mon Sep 17 00:00:00 2001 From: early evening Date: Thu, 11 May 2023 10:49:46 -0700 Subject: [PATCH] fix(docmaps-sdk): add @context when missing (#54) --- packages/ts-sdk/src/test/types.test.ts | 40 +++++++++++++++++++------- packages/ts-sdk/src/types.ts | 6 ++++ 2 files changed, 35 insertions(+), 11 deletions(-) diff --git a/packages/ts-sdk/src/test/types.test.ts b/packages/ts-sdk/src/test/types.test.ts index 26f80e93..c2819f72 100644 --- a/packages/ts-sdk/src/test/types.test.ts +++ b/packages/ts-sdk/src/test/types.test.ts @@ -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[], len: number) { +function isRightArray( + t: ExecutionContext, + arr: E.Either[], + 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) => { @@ -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') + }) }) diff --git a/packages/ts-sdk/src/types.ts b/packages/ts-sdk/src/types.ts index 03ef781e..36193e59 100644 --- a/packages/ts-sdk/src/types.ts +++ b/packages/ts-sdk/src/types.ts @@ -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[]) { @@ -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'),