Skip to content

Commit

Permalink
Align params on validation with strictValues
Browse files Browse the repository at this point in the history
  • Loading branch information
rubensworks committed Mar 16, 2020
1 parent 85606a0 commit 521e887
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 32 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ Optionally, the following parameters can be set in the `JsonLdParser` constructo
* `documentLoader` A custom loader for fetching remote contexts. This can be set to anything that implements [`IDocumentLoader`](https://github.com/rubensworks/jsonld-context-parser.js/blob/master/lib/IDocumentLoader.ts) _(Default: [`FetchDocumentLoader`](https://github.com/rubensworks/jsonld-context-parser.js/blob/master/lib/FetchDocumentLoader.ts))_
* `produceGeneralizedRdf`: If blank node predicates should be allowed, they will be ignored otherwise. _(Default: `false`)_
* `processingMode`: The maximum JSON-LD version that should be processable by this parser. _(Default: `1.0`)_
* `errorOnInvalidIris`: By default, JSON-LD requires that all properties (or @id's) that are not URIs, are unknown keywords, and do not occur in the context should be silently dropped. When setting this value to true, an error will be thrown when such properties occur. This is useful for debugging JSON-LD documents. _(Default: `false`)_
* `strictValues`: By default, JSON-LD requires that all properties (or @id's) that are not URIs, are unknown keywords, and do not occur in the context should be silently dropped. When setting this value to true, an error will be thrown when such properties occur. This also applies to invalid values such as language tags. This is useful for debugging JSON-LD documents. _(Default: `false`)_
* `allowSubjectList`: If RDF lists can appear in the subject position. _(Default: `false`)_
* `validateValueIndexes`: If @index inside array nodes should be validated. I.e., nodes inside the same array with the same @id, should have equal @index values. This is not applicable to this parser as we don't do explicit flattening, but it is required to be spec-compliant. _(Default: `false`)_
* `defaultGraph`: The default graph for constructing [quads](http://rdf.js.org/#dom-datafactory-quad). _(Default: `defaultGraph()`)_
Expand Down
13 changes: 4 additions & 9 deletions lib/JsonLdParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -493,9 +493,12 @@ export interface IJsonLdParserOptions {
* should be silently dropped.
* When setting this value to true,
* an error will be thrown when such properties occur.
*
* This also applies to invalid values such as language tags.
*
* Defaults to false.
*/
errorOnInvalidIris?: boolean;
strictValues?: boolean;
/**
* If RDF lists can appear in the subject position.
* Defaults to false.
Expand All @@ -513,14 +516,6 @@ export interface IJsonLdParserOptions {
* this must be explicitly set to true.
*/
validateValueIndexes?: boolean;
/**
* If values should be strictly checked.
* If true, an error will be thrown on invalid value ranged.
* if false, the resulting triple/quad will be omitted.
*
* Defaults to false.
*/
strictRanges?: boolean;
/**
* The graph to use as default graph when no explicit @graph is set.
* Defaults to dataFactory.defaultGraph().
Expand Down
6 changes: 2 additions & 4 deletions lib/ParsingContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,8 @@ export class ParsingContext {
public readonly produceGeneralizedRdf: boolean;
public readonly allowSubjectList: boolean;
public readonly processingMode: string;
public readonly errorOnInvalidProperties: boolean;
public readonly strictValues: boolean;
public readonly validateValueIndexes: boolean;
public readonly strictRanges: boolean;
public readonly rootContext: Promise<IJsonLdContextNormalized>;
public readonly defaultGraph?: RDF.NamedNode | RDF.BlankNode | RDF.DefaultGraph;
public readonly rdfDirection?: 'i18n-datatype' | 'compound-literal';
Expand Down Expand Up @@ -84,9 +83,8 @@ export class ParsingContext {
this.produceGeneralizedRdf = !!options.produceGeneralizedRdf;
this.allowSubjectList = !!options.allowSubjectList;
this.processingMode = options.processingMode || JsonLdParser.DEFAULT_PROCESSING_MODE;
this.errorOnInvalidProperties = !!options.errorOnInvalidIris;
this.strictValues = !!options.strictValues;
this.validateValueIndexes = !!options.validateValueIndexes;
this.strictRanges = !!options.strictRanges;
this.defaultGraph = options.defaultGraph;
this.rdfDirection = options.rdfDirection;
this.normalizeLanguageTags = options.normalizeLanguageTags;
Expand Down
10 changes: 5 additions & 5 deletions lib/Util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ export class Util {
`When an '@language' is set, the value of '@value' must be a string, got '${JSON.stringify(val)}'`);
}

if (!ContextParser.validateLanguage(valueLanguage, this.parsingContext.strictRanges)) {
if (!ContextParser.validateLanguage(valueLanguage, this.parsingContext.strictValues)) {
return [];
}

Expand All @@ -311,7 +311,7 @@ export class Util {
`When an '@direction' is set, the value of '@value' must be a string, got '${JSON.stringify(val)}'`);
}

if (!ContextParser.validateDirection(valueDirection, this.parsingContext.strictRanges)) {
if (!ContextParser.validateDirection(valueDirection, this.parsingContext.strictValues)) {
return [];
}
}
Expand Down Expand Up @@ -486,7 +486,7 @@ export class Util {
if (Util.isValidIri(expanded)) {
return this.dataFactory.namedNode(expanded);
} else {
if (expanded && this.parsingContext.errorOnInvalidProperties) {
if (expanded && this.parsingContext.strictValues) {
this.parsingContext.emitError(new ErrorCoded(`Invalid predicate IRI: ${expanded}`,
ERROR_CODES.INVALID_IRI_MAPPING));
} else {
Expand All @@ -510,7 +510,7 @@ export class Util {
}
const iri = ContextParser.expandTerm(key, context, false, this.parsingContext.getExpandOptions());
if (!Util.isValidIri(iri)) {
if (iri && this.parsingContext.errorOnInvalidProperties) {
if (iri && this.parsingContext.strictValues) {
this.parsingContext.emitError(new Error(`Invalid resource IRI: ${iri}`));
} else {
return null;
Expand All @@ -537,7 +537,7 @@ export class Util {
expanded = ContextParser.expandTerm(key, context, false, expandOptions);
}
if (!Util.isValidIri(expanded)) {
if (expanded && this.parsingContext.errorOnInvalidProperties) {
if (expanded && this.parsingContext.strictValues) {
this.parsingContext.emitError(new Error(`Invalid term IRI: ${expanded}`));
} else {
return null;
Expand Down
2 changes: 1 addition & 1 deletion lib/entryhandler/EntryHandlerInvalidFallback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {IEntryHandler} from "./IEntryHandler";

/**
* A catch-all for properties, that will either emit an error or ignore,
* depending on whether or not the `errorOnInvalidIris` property is set.
* depending on whether or not the `strictValues` property is set.
*/
export class EntryHandlerInvalidFallback implements IEntryHandler<boolean> {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {IEntryHandler} from "../IEntryHandler";

/**
* A catch-all for keywords, that will either emit an error or ignore,
* depending on whether or not the `errorOnInvalidIris` property is set.
* depending on whether or not the `strictValues` property is set.
*/
export class EntryHandlerKeywordUnknownFallback implements IEntryHandler<boolean> {

Expand Down Expand Up @@ -53,7 +53,7 @@ export class EntryHandlerKeywordUnknownFallback implements IEntryHandler<boolean
if (keywordType && typeof value !== keywordType) {
parsingContext.emitError(new Error(`Invalid value type for '${key}' with value '${value}'`));
}
} else if (parsingContext.errorOnInvalidProperties) {
} else if (parsingContext.strictValues) {
parsingContext.emitError(new Error(`Unknown keyword '${key}' with value '${value}'`));
}
parsingContext.emittedStack[depth] = false;
Expand Down
8 changes: 4 additions & 4 deletions test/JsonLdParser-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11106,11 +11106,11 @@ describe('JsonLdParser', () => {
});
});

describe('when instantiated with errorOnInvalidIris true', () => {
describe('when instantiated with strictValues true', () => {
let parser;

beforeEach(() => {
parser = new JsonLdParser({ errorOnInvalidIris: true });
parser = new JsonLdParser({ strictValues: true });
});

it('should error on an unknown keyword', async () => {
Expand Down Expand Up @@ -11182,7 +11182,7 @@ describe('JsonLdParser', () => {
"@index": "baz"
}
]`);
parser = new JsonLdParser({ errorOnInvalidIris: true, validateValueIndexes: true });
parser = new JsonLdParser({ strictValues: true, validateValueIndexes: true });
return expect(arrayifyStream(stream.pipe(parser))).rejects
.toEqual(new Error('Conflicting @index value for http://example/foo'));
});
Expand Down Expand Up @@ -11253,7 +11253,7 @@ describe('JsonLdParser', () => {
let contextListener;

beforeEach(() => {
parser = new JsonLdParser({errorOnInvalidIris: true});
parser = new JsonLdParser({strictValues: true});
contextListener = jest.fn();
parser.on('context', contextListener);
});
Expand Down
10 changes: 4 additions & 6 deletions test/Util-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -429,8 +429,8 @@ describe('Util', () => {
.resolves.toEqual([]);
});

it('with a @value and invalid @language should throw an error when strictRanges is true', async () => {
util.parsingContext.strictRanges = true;
it('with a @value and invalid @language should throw an error when strictValues is true', async () => {
util.parsingContext.strictValues = true;
return expect(util.valueToTerm(context, 'key', { '@value': 'abc', '@language': 'en us' }, 0, []))
.rejects.toThrow(new Error('The value of an \'@language\' must be a valid language tag, got \'"en us"\''));
});
Expand All @@ -451,20 +451,18 @@ describe('Util', () => {
.resolves.toEqual([]);
});

it('with a @value and invalid @direction should throw an error when strictRanges is true', async () => {
util.parsingContext.strictRanges = true;
it('with a @value and invalid @direction should throw an error when strictValues is true', async () => {
util.parsingContext.strictValues = true;
return expect(util.valueToTerm(context, 'key', { '@value': 'abc', '@direction': 'r tl' }, 0, []))
.rejects.toThrow(new Error('The value of an \'@direction\' must be \'ltr\' or \'rtl\', got \'"r tl"\''));
});

it('with a @value and valid @direction rtl should return a plain literal', async () => {
util.parsingContext.strictRanges = true;
return expect(util.valueToTerm(context, 'key', { '@value': 'abc', '@direction': 'rtl' }, 0, []))
.resolves.toEqualRdfTermArray([literal('abc')]);
});

it('with a @value and valid @direction ltr should return a plain literal', async () => {
util.parsingContext.strictRanges = true;
return expect(util.valueToTerm(context, 'key', { '@value': 'abc', '@direction': 'ltr' }, 0, []))
.resolves.toEqualRdfTermArray([literal('abc')]);
});
Expand Down

0 comments on commit 521e887

Please sign in to comment.