Skip to content

Commit

Permalink
Handle @type: @id in property-based index containers
Browse files Browse the repository at this point in the history
  • Loading branch information
rubensworks committed Mar 16, 2020
1 parent 6386ae8 commit 462d790
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 1 deletion.
18 changes: 17 additions & 1 deletion lib/containerhandler/ContainerHandlerIndex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,26 @@ export class ContainerHandlerIndex implements IContainerHandler {
ERROR_CODES.INVALID_TERM_DEFINITION);
}

// When @index is used, values must be node values, unless @type: @id is defined in the context
if (typeof value !== 'object') {
// Error if we don't have @type: @id
if (Util.getContextValueType(context, indexKey) !== '@id') {
throw new ErrorCoded(
`Property-based index containers require nodes as values or strings with @type: @id, but got: ${value}`,
ERROR_CODES.INVALID_VALUE_OBJECT);
}

// Add an @id to the stack, so our expanded @index value can make use of it
const id = util.resourceToTerm(context, value);
if (id) {
parsingContext.idStack[depth + 1] = [id];
}
}

// Expand the @index value
const indexProperty = util.createVocabOrBaseTerm(context, indexPropertyRaw);
if (indexProperty) {
const indexValues = await util.valueToTerm(context, indexKey,
const indexValues = await util.valueToTerm(context, <any> null, // No specific key applies here, so pass null
await util.getContainerKey(keys[depth], keys, depth), depth, keys);
for (const indexValue of indexValues) {
await EntryHandlerPredicate.handlePredicateObject(parsingContext, util, keys, depth + 1,
Expand Down
88 changes: 88 additions & 0 deletions test/JsonLdParser-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6913,6 +6913,94 @@ describe('JsonLdParser', () => {
ERROR_CODES.INVALID_TERM_DEFINITION));
});

it('with @id and index map with a raw value should error', async () => {
const stream = streamifyString(`
{
"@context": {
"ex": "http://ex.org/",
"p": { "@id": "http://ex.org/pred1", "@container": "@index", "@index": "ex:prop" }
},
"@id": "http://ex.org/myid",
"p": {
"Value1": "ex:id1"
}
}`);
return expect(arrayifyStream(stream.pipe(parser))).rejects.toThrow(
new ErrorCoded('Property-based index containers require nodes as values or strings with ' +
'@type: @id, but got: ex:id1',
ERROR_CODES.INVALID_VALUE_OBJECT));
});

it('with @id and index map with a raw value in an array should error', async () => {
const stream = streamifyString(`
{
"@context": {
"ex": "http://ex.org/",
"p": { "@id": "http://ex.org/pred1", "@container": "@index", "@index": "ex:prop" }
},
"@id": "http://ex.org/myid",
"p": {
"Value1": [ "ex:id1" ]
}
}`);
return expect(arrayifyStream(stream.pipe(parser))).rejects.toThrow(
new ErrorCoded('Property-based index containers require nodes as values or strings with ' +
'@type: @id, but got: ex:id1',
ERROR_CODES.INVALID_VALUE_OBJECT));
});

it('with @id and index map with a raw value with @type: @id', async () => {
const stream = streamifyString(`
{
"@context": {
"ex": "http://ex.org/",
"p": { "@id": "http://ex.org/pred1", "@type": "@id", "@container": "@index", "@index": "ex:prop" }
},
"@id": "http://ex.org/myid",
"p": {
"Value1": "ex:id1"
}
}`);
return expect(await arrayifyStream(stream.pipe(parser))).toBeRdfIsomorphic([
triple(namedNode('http://ex.org/id1'), namedNode('http://ex.org/prop'),
literal('Value1')),
triple(namedNode('http://ex.org/myid'), namedNode('http://ex.org/pred1'),
namedNode('http://ex.org/id1')),
]);
});

it('with @id and index map with a raw value with @type: @id with invalid IRI', async () => {
const stream = streamifyString(`
{
"@context": {
"ex": "http://ex.org/",
"p": { "@id": "http://ex.org/pred1", "@type": "@id", "@container": "@index", "@index": "ex:prop" }
},
"@id": "http://ex.org/myid",
"p": {
"Value1": "id1"
}
}`);
return expect(await arrayifyStream(stream.pipe(parser))).toBeRdfIsomorphic([]);
});

it('with @id and index map with a raw value with @type: @bla should error', async () => {
const stream = streamifyString(`
{
"@context": {
"ex": "http://ex.org/",
"p": { "@id": "http://ex.org/pred1", "@type": "@bla", "@container": "@index", "@index": "ex:prop" }
},
"@id": "http://ex.org/myid",
"p": {
"Value1": "ex:id1"
}
}`);
return expect(arrayifyStream(stream.pipe(parser))).rejects.toThrow(
new ErrorCoded('A context @type must be an absolute IRI, found: \'p\': \'@bla\'',
ERROR_CODES.INVALID_TYPE_MAPPING));
});

});

describe('for identifiers', () => {
Expand Down

0 comments on commit 462d790

Please sign in to comment.