Skip to content

Sealed contexts (plus restrict context nullification to term definitions) #289

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

Merged
merged 10 commits into from
Apr 17, 2019
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
### Added
- Testing: `skip` and `only` flags in manifests.
- Testing: `VERBOSE_SKIP=true` env var to debug skipping.
- Support `@protected` and `protectedMode`.

## 1.5.4 - 2019-02-28

Expand Down
31 changes: 20 additions & 11 deletions lib/compact.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ const JsonLdError = require('./JsonLdError');
const {
isArray: _isArray,
isObject: _isObject,
isString: _isString
isString: _isString,
isUndefined: _isUndefined
} = require('./types');

const {
Expand Down Expand Up @@ -100,8 +101,13 @@ api.compact = ({

// use any scoped context on activeProperty
const ctx = _getContextValue(activeCtx, activeProperty, '@context');
if(ctx) {
activeCtx = _processContext({activeCtx, localCtx: ctx, options});
if(!_isUndefined(ctx)) {
// Note: spec's `from term` var is named `isPropertyTermScopedContext`
activeCtx = _processContext({
activeCtx,
localCtx: ctx,
options: {...options, isPropertyTermScopedContext: true}
});
}

// recursively compact object
Expand All @@ -119,7 +125,7 @@ api.compact = ({
// do value compaction on @values and subject references
if(_isValue(element) || _isSubjectReference(element)) {
const rval =
api.compactValue({activeCtx, activeProperty, value: element});
api.compactValue({activeCtx, activeProperty, value: element, options});
if(options.link && _isSubjectReference(element)) {
// store linked element
if(!(element['@id'] in options.link)) {
Expand Down Expand Up @@ -156,7 +162,7 @@ api.compact = ({

// Use any scoped context defined on this value
const ctx = _getContextValue(activeCtx, compactedType, '@context');
if(ctx) {
if(!_isUndefined(ctx)) {
activeCtx = _processContext({activeCtx, localCtx: ctx, options});
}
}
Expand Down Expand Up @@ -294,7 +300,7 @@ api.compact = ({
activeCtx.mappings[itemActiveProperty]['@nest'] : null;
let nestResult = rval;
if(nestProperty) {
_checkNestProperty(activeCtx, nestProperty);
_checkNestProperty(activeCtx, nestProperty, options);
if(!_isObject(rval[nestProperty])) {
rval[nestProperty] = {};
}
Expand Down Expand Up @@ -323,7 +329,7 @@ api.compact = ({
activeCtx.mappings[itemActiveProperty]['@nest'] : null;
let nestResult = rval;
if(nestProperty) {
_checkNestProperty(activeCtx, nestProperty);
_checkNestProperty(activeCtx, nestProperty, options);
if(!_isObject(rval[nestProperty])) {
rval[nestProperty] = {};
}
Expand Down Expand Up @@ -794,10 +800,11 @@ api.compactIri = ({
* @param activeCtx the active context.
* @param activeProperty the active property that points to the value.
* @param value the value to compact.
* @param {Object} [options] - processing options.
*
* @return the compaction result.
*/
api.compactValue = ({activeCtx, activeProperty, value}) => {
api.compactValue = ({activeCtx, activeProperty, value, options}) => {
// value is a @value
if(_isValue(value)) {
// get context rules
Expand Down Expand Up @@ -872,7 +879,8 @@ api.compactValue = ({activeCtx, activeProperty, value}) => {
}

// value is a subject reference
const expandedProperty = _expandIri(activeCtx, activeProperty, {vocab: true});
const expandedProperty = _expandIri(activeCtx, activeProperty, {vocab: true},
options);
const type = _getContextValue(activeCtx, activeProperty, '@type');
const compacted = api.compactIri(
{activeCtx, iri: value['@id'], relativeTo: {vocab: type === '@vocab'}});
Expand Down Expand Up @@ -1055,9 +1063,10 @@ function _selectTerm(
*
* @param activeCtx the active context.
* @param nestProperty a term in the active context or `@nest`.
* @param {Object} [options] - processing options.
*/
function _checkNestProperty(activeCtx, nestProperty) {
if(_expandIri(activeCtx, nestProperty, {vocab: true}) !== '@nest') {
function _checkNestProperty(activeCtx, nestProperty, options) {
if(_expandIri(activeCtx, nestProperty, {vocab: true}, options) !== '@nest') {
throw new JsonLdError(
'JSON-LD compact error; nested property must have an @nest value ' +
'resolving to @nest.',
Expand Down
Loading