@fluree/fluree-context-utils
Advanced tools
Comparing version 1.0.0 to 1.0.1
@@ -15,5 +15,6 @@ const jsonld = require('jsonld'); | ||
if (!options.errorOnInvalidUrl) { | ||
const newContext = jsonldDoc['@context'].filter( | ||
(x) => x !== err.details.url | ||
); | ||
const context = Array.isArray(jsonldDoc['@context']) | ||
? jsonldDoc['@context'] | ||
: [jsonldDoc['@context']]; | ||
const newContext = context.filter((x) => x !== err.details.url); | ||
return await attemptExpanded({ | ||
@@ -24,2 +25,5 @@ ...jsonldDoc, | ||
} else { | ||
if (err?.details?.url) { | ||
err.url = err.details.url; | ||
} | ||
throw err; | ||
@@ -361,4 +365,5 @@ } | ||
const expanded = await attemptExpanded(jsonldDoc, options); | ||
const pullVocabulary = (url) => | ||
axios | ||
const pullVocabulary = (url) => { | ||
return axios | ||
.get(url) | ||
@@ -379,2 +384,7 @@ .then((res) => { | ||
if (options.errorOnUndefinedProperties) { | ||
// if (!isUrl(url)) { | ||
// throw new TypeError( | ||
// `Could not retrieve ` | ||
// ) | ||
// } | ||
throw new TypeError( | ||
@@ -387,2 +397,3 @@ `Could not retrieve property vocabulary document at ${url}` | ||
}); | ||
}; | ||
@@ -401,3 +412,15 @@ const customReducer = (graphArray) => | ||
if (key === '@type') { | ||
const result = await pullVocabulary(expandedDoc[key]); | ||
let result; | ||
if (Array.isArray(expandedDoc[key])) { | ||
if (expandedDoc[key].some((x) => !isUrl(x))) { | ||
return; | ||
} | ||
result = await Promise.all(expandedDoc[key].map(pullVocabulary)); | ||
result = result.filter((x) => x['@graph'].length > 0); | ||
} else { | ||
if (!isUrl(expandedDoc[key])) { | ||
return; | ||
} | ||
result = await pullVocabulary(expandedDoc[key]); | ||
} | ||
interimContext = { | ||
@@ -404,0 +427,0 @@ ...interimContext, |
112
lib/tests.js
const flureeContext = require('./index'); | ||
const jsonldDoc = { | ||
'@context': 'https://schema.org', | ||
//note this invalid URL that will cause an | ||
//invalid URL error if errorOnInvalidUrl is TRUE: | ||
'@context': 'https://woop-woop-not-a-site.org', | ||
'@id': 'https://www.wikidata.org/wiki/Q836821', | ||
'@type': ['Movie'], | ||
name: "The Hitchhiker's Guide to the Galaxy", | ||
//note this invalid property that will not be included in returned, validated result: | ||
noPropertyDescribedBySchemaDotOrg: 'foobar', | ||
isBasedOn: { | ||
@@ -17,95 +17,13 @@ '@id': 'https://www.wikidata.org/wiki/Q3107329', | ||
// Validate a JSON-LD payload by dereferencing the context definition from a URL such as https://schema.org | ||
flureeContext.includeVocabulary(jsonldDoc).then((validatedJsonldDoc) => { | ||
console.log(JSON.stringify(validatedJsonldDoc, null, 2)); | ||
/* | ||
Output: | ||
{ | ||
'@context': [ | ||
'https://schema.org', | ||
{ | ||
brick: 'https://brickschema.org/schema/Brick#', | ||
csvw: 'http://www.w3.org/ns/csvw#', | ||
dc: 'http://purl.org/dc/elements/1.1/', | ||
dcam: 'http://purl.org/dc/dcam/', | ||
dcat: 'http://www.w3.org/ns/dcat#', | ||
dcmitype: 'http://purl.org/dc/dcmitype/', | ||
dcterms: 'http://purl.org/dc/terms/', | ||
doap: 'http://usefulinc.com/ns/doap#', | ||
foaf: 'http://xmlns.com/foaf/0.1/', | ||
geo: 'http://www.opengis.net/ont/geosparql#', | ||
odrl: 'http://www.w3.org/ns/odrl/2/', | ||
org: 'http://www.w3.org/ns/org#', | ||
owl: 'http://www.w3.org/2002/07/owl#', | ||
prof: 'http://www.w3.org/ns/dx/prof/', | ||
prov: 'http://www.w3.org/ns/prov#', | ||
qb: 'http://purl.org/linked-data/cube#', | ||
rdf: 'http://www.w3.org/1999/02/22-rdf-syntax-ns#', | ||
rdfs: 'http://www.w3.org/2000/01/rdf-schema#', | ||
schema: 'https://schema.org/', | ||
sh: 'http://www.w3.org/ns/shacl#', | ||
skos: 'http://www.w3.org/2004/02/skos/core#', | ||
sosa: 'http://www.w3.org/ns/sosa/', | ||
ssn: 'http://www.w3.org/ns/ssn/', | ||
time: 'http://www.w3.org/2006/time#', | ||
vann: 'http://purl.org/vocab/vann/', | ||
void: 'http://rdfs.org/ns/void#', | ||
wgs: 'https://www.w3.org/2003/01/geo/wgs84_pos#', | ||
xsd: 'http://www.w3.org/2001/XMLSchema#', | ||
}, | ||
], | ||
'@graph': [ | ||
{ | ||
'@id': 'https://www.wikidata.org/wiki/Q836821', | ||
'@type': ['Movie'], | ||
name: "The Hitchhiker's Guide to the Galaxy", | ||
noPropertyDescribedBySchemaDotOrg: 'foobar', | ||
isBasedOn: { | ||
'@id': 'https://www.wikidata.org/wiki/Q3107329', | ||
'@type': 'Book', | ||
name: "The Hitchhiker's Guide to the Galaxy", | ||
}, | ||
}, | ||
{ | ||
'@id': 'schema:copyrightNotice', | ||
'@type': 'rdf:Property', | ||
'rdfs:comment': | ||
'Text of a notice appropriate for describing the copyright aspects of this Creative Work, ideally indicating the owner of the copyright for the Work.', | ||
'rdfs:label': 'copyrightNotice', | ||
'schema:domainIncludes': { | ||
'@id': 'schema:CreativeWork', | ||
}, | ||
'schema:isPartOf': { | ||
'@id': 'https://pending.schema.org', | ||
}, | ||
'schema:rangeIncludes': { | ||
'@id': 'schema:Text', | ||
}, | ||
'schema:source': { | ||
'@id': 'https://github.com/schemaorg/schemaorg/issues/2659', | ||
}, | ||
}, | ||
{ | ||
'@id': 'schema:spatialCoverage', | ||
'@type': 'rdf:Property', | ||
'owl:equivalentProperty': { | ||
'@id': 'dcterms:spatial', | ||
}, | ||
'rdfs:comment': | ||
'The spatialCoverage of a CreativeWork indicates the place(s) which are the focus of the content. It is a subproperty of\n contentLocation intended primarily for more technical and detailed materials. For example with a Dataset, it indicates\n areas that the dataset describes: a dataset of New York weather would have spatialCoverage which was the place: the state of New York.', | ||
'rdfs:label': 'spatialCoverage', | ||
'rdfs:subPropertyOf': { | ||
'@id': 'schema:contentLocation', | ||
}, | ||
'schema:domainIncludes': { | ||
'@id': 'schema:CreativeWork', | ||
}, | ||
'schema:rangeIncludes': { | ||
'@id': 'schema:Place', | ||
}, | ||
}, | ||
//... | ||
], | ||
}; | ||
*/ | ||
}); | ||
const validatedJsonldDoc = flureeContext | ||
.includeVocabulary(jsonldDoc, { | ||
errorOnInvalidUrl: true, | ||
}) | ||
.catch((err) => { | ||
console.log(err); | ||
/* | ||
Output: | ||
TypeError: Could not retrieve property vocabulary document at | ||
http://schema.org/noPropertyDescribedBySchemaDotOrg | ||
*/ | ||
}); |
{ | ||
"name": "@fluree/fluree-context-utils", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "NodeJS Library helping to dereference context and vocabulary documents from URLs, particularly for use with JSON-LD FlureeDB.", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
258
README.md
@@ -23,3 +23,13 @@ # flureeContext | ||
> Note: default `options` are as follows (explanations of their utility are described below): | ||
```js | ||
{ | ||
errorOnInvalid: false, | ||
errorOnLookupFailure: false, | ||
expandContext: true, | ||
} | ||
``` | ||
```js | ||
const flureeContext = require('@fluree/fluree-context-utils'); | ||
@@ -32,3 +42,4 @@ | ||
name: "The Hitchhiker's Guide to the Galaxy", | ||
//note this invalid property that will not be included in returned, validated result: | ||
//note this invalid property that will not be included | ||
//in returned, validated result: | ||
noPropertyDescribedBySchemaDotOrg: 'foobar', | ||
@@ -42,3 +53,4 @@ isBasedOn: { | ||
// Validate a JSON-LD payload by dereferencing the context definition from a URL such as https://schema.org | ||
// Validate a JSON-LD payload by dereferencing the context | ||
//definition from a URL such as https://schema.org | ||
const validatedJsonldDoc = await flureeContext.validate(jsonldDoc); | ||
@@ -74,4 +86,8 @@ | ||
- errorOnLookupFailure | ||
#### errorOnLookupFailure | ||
True to throw error if one or more context documents provided as URLs cannot be retrieved. If false, validation will continue against retrievable context documents. (Default: False) | ||
An example of using `errorOnLookupFailure: true` | ||
```js | ||
@@ -103,6 +119,8 @@ const flureeContext = require('@fluree/fluree-context-utils'); | ||
- | ||
#### errorOnInvalid | ||
### isValid | ||
True to throw error if one or more properties or types cannot be validated against context map. If false, validation will continue and only valid properties or types will be returned by function (Default: False) | ||
An example of using `errorOnInvalid: true` | ||
```js | ||
@@ -112,2 +130,33 @@ const flureeContext = require('@fluree/fluree-context-utils'); | ||
const jsonldDoc = { | ||
'@context': ['https://schema.org'], | ||
'@id': 'https://www.wikidata.org/wiki/Q836821', | ||
'@type': ['Movie'], | ||
name: "The Hitchhiker's Guide to the Galaxy", | ||
//note this invalid property that will throw an | ||
//error when errorOnInvalid is set to TRUE: | ||
noPropertyDescribedBySchemaDotOrg: 'foobar', | ||
}; | ||
try { | ||
const validatedJsonldDoc = await flureeContext.validate(jsonldDoc, { | ||
errorOnInvalid: true, | ||
}); | ||
} catch (error) { | ||
console.log(JSON.stringify(error, null, 2)); | ||
/* Output: | ||
TypeError: The following properties failed validation against the provided '@context': noPropertyDescribedBySchemaDotOrg | ||
*/ | ||
} | ||
``` | ||
#### expandContext | ||
True to replace context URLs with retrieved object map. False to leave context document as is (Default: True) | ||
An example of using `expandContext: false` | ||
```js | ||
const flureeContext = require('@fluree/fluree-context-utils'); | ||
const jsonldDoc = { | ||
'@context': 'https://schema.org', | ||
@@ -117,4 +166,6 @@ '@id': 'https://www.wikidata.org/wiki/Q836821', | ||
name: "The Hitchhiker's Guide to the Galaxy", | ||
//note this invalid property that will cause an invalid result: | ||
//note this invalid property that will not be included | ||
//in returned, validated result: | ||
noPropertyDescribedBySchemaDotOrg: 'foobar', | ||
isBasedOn: { | ||
@@ -127,22 +178,24 @@ '@id': 'https://www.wikidata.org/wiki/Q3107329', | ||
flureeContext | ||
.isValid(jsonldDoc) | ||
.then((isValid) => { | ||
console.log({ isValid }); | ||
/* | ||
Output: | ||
{ isValid: false } | ||
*/ | ||
}) | ||
.then((_) => { | ||
delete jsonldDoc.noPropertyDescribedBySchemaDotOrg; | ||
}) | ||
.then((_) => flureeContext.isValid(jsonldDoc)) | ||
.then((isValid) => { | ||
console.log({ isValid }); | ||
/* | ||
Output: | ||
{ isValid: true } | ||
*/ | ||
}); | ||
// Validate a JSON-LD payload by dereferencing the context definition from a URL such as https://schema.org | ||
const validatedJsonldDoc = await flureeContext.validate(jsonldDoc, { | ||
expandContext: false, | ||
}); | ||
console.log(JSON.stringify(x, null, 2)); | ||
/* | ||
Output: | ||
{ | ||
"@context": "https://schema.org", | ||
"@id": "https://www.wikidata.org/wiki/Q836821", | ||
"@type": [ | ||
"Movie" | ||
], | ||
"name": "The Hitchhiker's Guide to the Galaxy", | ||
"isBasedOn": { | ||
"@id": "https://www.wikidata.org/wiki/Q3107329", | ||
"@type": "Book", | ||
"name": "The Hitchhiker's Guide to the Galaxy" | ||
} | ||
} | ||
*/ | ||
``` | ||
@@ -152,3 +205,14 @@ | ||
Accepts a JSON-LD document and attempts to retrieve the underlying vocabulary definitions for the types and properties defined by valid URLs. Returns the JSON-LD document enriched with those definitions. | ||
> Note: default `options` are as follows (explanations of their utility are described below): | ||
```js | ||
{ | ||
errorOnUndefinedProperties: false, | ||
errorOnInvalidUrl: false | ||
} | ||
``` | ||
```js | ||
const flureeContext = require('@fluree/fluree-context-utils'); | ||
@@ -161,4 +225,2 @@ | ||
name: "The Hitchhiker's Guide to the Galaxy", | ||
//note this invalid property that will not be included in returned, validated result: | ||
noPropertyDescribedBySchemaDotOrg: 'foobar', | ||
isBasedOn: { | ||
@@ -241,22 +303,5 @@ '@id': 'https://www.wikidata.org/wiki/Q3107329', | ||
}, | ||
{ | ||
'@id': 'schema:spatialCoverage', | ||
'@type': 'rdf:Property', | ||
'owl:equivalentProperty': { | ||
'@id': 'dcterms:spatial', | ||
}, | ||
'rdfs:comment': | ||
'The spatialCoverage of a CreativeWork indicates the place(s) which are the focus of the content. It is a subproperty of\n contentLocation intended primarily for more technical and detailed materials. For example with a Dataset, it indicates\n areas that the dataset describes: a dataset of New York weather would have spatialCoverage which was the place: the state of New York.', | ||
'rdfs:label': 'spatialCoverage', | ||
'rdfs:subPropertyOf': { | ||
'@id': 'schema:contentLocation', | ||
}, | ||
'schema:domainIncludes': { | ||
'@id': 'schema:CreativeWork', | ||
}, | ||
'schema:rangeIncludes': { | ||
'@id': 'schema:Place', | ||
}, | ||
}, | ||
//... | ||
//... | ||
//... | ||
], | ||
@@ -267,1 +312,120 @@ }; | ||
``` | ||
Options: | ||
#### errorOnUndefinedProperties | ||
True to throw error if one or more expanded types/properties described by a valid URL but no vocabulary document is available for lookup at that URL. (Default: False) | ||
An example of using `errorOnUndefinedProperties: true` | ||
```js | ||
const flureeContext = require('@fluree/fluree-context-utils'); | ||
const jsonldDoc = { | ||
'@context': 'https://schema.org', | ||
'@id': 'https://www.wikidata.org/wiki/Q836821', | ||
'@type': ['Movie'], | ||
name: "The Hitchhiker's Guide to the Galaxy", | ||
//note this invalid property that will cause an | ||
//undefined property error if errorOnUndefinedProperties is TRUE: | ||
noPropertyDescribedBySchemaDotOrg: 'foobar', | ||
isBasedOn: { | ||
'@id': 'https://www.wikidata.org/wiki/Q3107329', | ||
'@type': 'Book', | ||
name: "The Hitchhiker's Guide to the Galaxy", | ||
}, | ||
}; | ||
const validatedJsonldDoc = flureeContext | ||
.includeVocabulary(jsonldDoc, { | ||
errorOnUndefinedProperties: true, | ||
}) | ||
.catch((err) => { | ||
console.log(err); | ||
/* | ||
Output: | ||
TypeError: Could not retrieve property vocabulary document at | ||
http://schema.org/noPropertyDescribedBySchemaDotOrg | ||
*/ | ||
}); | ||
``` | ||
#### errorOnInvalidUrl | ||
True to throw error if one or more properties or types is described by an invalid URL. False to proceed without throwing error (Default: False) | ||
An example of using `errorOnInvalidUrl: true` | ||
```js | ||
const flureeContext = require('@fluree/fluree-context-utils'); | ||
const jsonldDoc = { | ||
//note this invalid URL that will cause an | ||
//invalid URL error if errorOnInvalidUrl is TRUE: | ||
'@context': 'https://woop-woop-not-a-site.org', | ||
'@id': 'https://www.wikidata.org/wiki/Q836821', | ||
'@type': ['Movie'], | ||
name: "The Hitchhiker's Guide to the Galaxy", | ||
isBasedOn: { | ||
'@id': 'https://www.wikidata.org/wiki/Q3107329', | ||
'@type': 'Book', | ||
name: "The Hitchhiker's Guide to the Galaxy", | ||
}, | ||
}; | ||
const validatedJsonldDoc = flureeContext | ||
.includeVocabulary(jsonldDoc, { | ||
errorOnInvalidUrl: true, | ||
}) | ||
.catch((err) => { | ||
console.log(err); | ||
/* | ||
Output: | ||
url: 'https://woop-woop-not-a-site.org' | ||
jsonld.InvalidUrl: Dereferencing a URL did not result in a valid JSON-LD object. Possible causes are an inaccessible URL perhaps due to a same-origin policy (ensure the server uses CORS if you are using client-side JavaScript), too many redirects, a non-JSON response, or more than one HTTP Link Header was provided for a remote context. | ||
*/ | ||
}); | ||
``` | ||
### isValid | ||
```js | ||
const flureeContext = require('@fluree/fluree-context-utils'); | ||
const jsonldDoc = { | ||
'@context': 'https://schema.org', | ||
'@id': 'https://www.wikidata.org/wiki/Q836821', | ||
'@type': ['Movie'], | ||
name: "The Hitchhiker's Guide to the Galaxy", | ||
//note this invalid property that will cause an invalid result: | ||
noPropertyDescribedBySchemaDotOrg: 'foobar', | ||
isBasedOn: { | ||
'@id': 'https://www.wikidata.org/wiki/Q3107329', | ||
'@type': 'Book', | ||
name: "The Hitchhiker's Guide to the Galaxy", | ||
}, | ||
}; | ||
flureeContext | ||
.isValid(jsonldDoc) | ||
.then((isValid) => { | ||
console.log({ isValid }); | ||
/* | ||
Output: | ||
{ isValid: false } | ||
*/ | ||
}) | ||
.then((_) => { | ||
delete jsonldDoc.noPropertyDescribedBySchemaDotOrg; | ||
}) | ||
.then((_) => flureeContext.isValid(jsonldDoc)) | ||
.then((isValid) => { | ||
console.log({ isValid }); | ||
/* | ||
Output: | ||
{ isValid: true } | ||
*/ | ||
}); | ||
``` |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
33064
420
603