Skip to content

Commit d709341

Browse files
gkelloggdavidlehn
authored andcommitted
@type may be used as a term definition only if "@container": "@set".
1 parent 3dfb2c0 commit d709341

File tree

4 files changed

+35
-15
lines changed

4 files changed

+35
-15
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
- Better checking of absolute IRIs.
1919
- Terms that begin with a ':' are not considered absolute or compact IRIs.
2020
- Don't use terms with `"@prefix": false` or expanded term definitions to construct compact IRIs.
21+
- "@type" may be used as a term definition only if "@container": "@set".
2122

2223
### Removed
2324
- **BREAKING**: Remove callback API support. This includes removing support

lib/compact.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ const {
2424
expandIri: _expandIri,
2525
getContextValue: _getContextValue,
2626
isKeyword: _isKeyword,
27-
process: _processContext
27+
process: _processContext,
28+
processingMode: _processingMode
2829
} = require('./context');
2930

3031
const {
@@ -220,7 +221,15 @@ api.compact = async ({
220221
// use keyword alias and add value
221222
const alias = api.compactIri(
222223
{activeCtx, iri: expandedProperty, relativeTo: {vocab: true}});
223-
const isArray = _isArray(compactedValue) && expandedValue.length === 0;
224+
const container = _getContextValue(
225+
activeCtx, alias, '@container') || [];
226+
227+
// treat as array for @type if @container includes @set
228+
const typeAsSet = isType &&
229+
container.includes('@set') &&
230+
_processingMode(activeCtx, 1.1);
231+
const isArray =
232+
(_isArray(compactedValue) && expandedValue.length === 0) || typeAsSet;
224233
_addValue(rval, alias, compactedValue, {propertyIsArray: isArray});
225234
continue;
226235
}

lib/context.js

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -315,14 +315,30 @@ api.createTermDefinition = (
315315
// now defining term
316316
defined.set(term, false);
317317

318-
if(api.isKeyword(term)) {
318+
// get context term value
319+
let value;
320+
if(localCtx.hasOwnProperty(term)) {
321+
value = localCtx[term];
322+
}
323+
324+
if(term === '@type' &&
325+
_isObject(value) &&
326+
value['@container'] === '@set' &&
327+
api.processingMode(activeCtx, 1.1)) {
328+
329+
const validKeys = ['@container', '@id', '@protected'];
330+
if(Object.keys(value).some(k => !validKeys.includes(k))) {
331+
throw new JsonLdError(
332+
'Invalid JSON-LD syntax; keywords cannot be overridden.',
333+
'jsonld.SyntaxError',
334+
{code: 'keyword redefinition', context: localCtx, term});
335+
}
336+
} else if(api.isKeyword(term)) {
319337
throw new JsonLdError(
320338
'Invalid JSON-LD syntax; keywords cannot be overridden.',
321339
'jsonld.SyntaxError',
322340
{code: 'keyword redefinition', context: localCtx, term});
323-
}
324-
325-
if(term === '') {
341+
} else if(term === '') {
326342
throw new JsonLdError(
327343
'Invalid JSON-LD syntax; a term cannot be an empty string.',
328344
'jsonld.SyntaxError',
@@ -337,12 +353,6 @@ api.createTermDefinition = (
337353
activeCtx.mappings.delete(term);
338354
}
339355

340-
// get context term value
341-
let value;
342-
if(localCtx.hasOwnProperty(term)) {
343-
value = localCtx[term];
344-
}
345-
346356
// clear context entry
347357
if(value === null || (_isObject(value) && value['@id'] === null)) {
348358
activeCtx.mappings.set(term, null);
@@ -468,6 +478,9 @@ api.createTermDefinition = (
468478
// term is an absolute IRI
469479
mapping['@id'] = term;
470480
}
481+
} else if(term == '@type') {
482+
// Special case, were we've previously determined that container is @set
483+
mapping['@id'] = term;
471484
} else {
472485
// non-IRIs *must* define @ids if @vocab is not available
473486
if(!('@vocab' in activeCtx)) {

tests/test-common.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,6 @@ const TEST_TYPES = {
3232
specVersion: ['json-ld-1.0'],
3333
// FIXME
3434
idRegex: [
35-
// type set
36-
/compact-manifest.jsonld#t0104$/,
37-
/compact-manifest.jsonld#t0105$/,
3835
// @type: @none
3936
/compact-manifest.jsonld#ttn01$/,
4037
/compact-manifest.jsonld#ttn02$/,

0 commit comments

Comments
 (0)