Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add @context when missing #54

Merged
merged 3 commits into from
May 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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