@twin.org/data-json-ld 0.0.1-next.30 → 0.0.1-next.32
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/index.cjs +181 -68
- package/dist/esm/index.mjs +183 -70
- package/dist/types/models/jsonLdTypes.d.ts +8 -0
- package/dist/types/utils/jsonLdProcessor.d.ts +6 -2
- package/docs/changelog.md +28 -0
- package/docs/reference/classes/JsonLdProcessor.md +12 -2
- package/docs/reference/variables/JsonLdTypes.md +12 -0
- package/package.json +2 -2
package/dist/cjs/index.cjs
CHANGED
|
@@ -85,6 +85,14 @@ const JsonLdTypes = {
|
|
|
85
85
|
* Represents JSON-LD Context Definition.
|
|
86
86
|
*/
|
|
87
87
|
ContextDefinition: "JsonLdContextDefinition",
|
|
88
|
+
/**
|
|
89
|
+
* Represents JSON-LD Context Definition Element.
|
|
90
|
+
*/
|
|
91
|
+
ContextDefinitionElement: "JsonLdContextDefinitionElement",
|
|
92
|
+
/**
|
|
93
|
+
* Represents JSON-LD Context Definition Root.
|
|
94
|
+
*/
|
|
95
|
+
ContextDefinitionRoot: "JsonLdContextDefinitionRoot",
|
|
88
96
|
/**
|
|
89
97
|
* Represents JSON-LD Expanded Term Definition.
|
|
90
98
|
*/
|
|
@@ -124,7 +132,7 @@ const JsonLdTypes = {
|
|
|
124
132
|
};
|
|
125
133
|
|
|
126
134
|
var type$e = "string";
|
|
127
|
-
var description$
|
|
135
|
+
var description$p = "Helper Types";
|
|
128
136
|
var JsonLdContainerTypeSchema = {
|
|
129
137
|
type: type$e,
|
|
130
138
|
"enum": [
|
|
@@ -134,10 +142,10 @@ var JsonLdContainerTypeSchema = {
|
|
|
134
142
|
"@graph",
|
|
135
143
|
"@type"
|
|
136
144
|
],
|
|
137
|
-
description: description$
|
|
145
|
+
description: description$p
|
|
138
146
|
};
|
|
139
147
|
|
|
140
|
-
var anyOf$
|
|
148
|
+
var anyOf$a = [
|
|
141
149
|
{
|
|
142
150
|
type: "array",
|
|
143
151
|
minItems: 2,
|
|
@@ -311,10 +319,10 @@ var anyOf$8 = [
|
|
|
311
319
|
maxItems: 2
|
|
312
320
|
}
|
|
313
321
|
];
|
|
314
|
-
var description$
|
|
322
|
+
var description$o = "Helper Types.";
|
|
315
323
|
var JsonLdContainerTypeArraySchema = {
|
|
316
|
-
anyOf: anyOf$
|
|
317
|
-
description: description$
|
|
324
|
+
anyOf: anyOf$a,
|
|
325
|
+
description: description$o
|
|
318
326
|
};
|
|
319
327
|
|
|
320
328
|
var type$d = "object";
|
|
@@ -388,11 +396,45 @@ var additionalProperties$b = {
|
|
|
388
396
|
}
|
|
389
397
|
]
|
|
390
398
|
};
|
|
391
|
-
var description$
|
|
399
|
+
var description$n = "A context definition defines a local context in a node object.";
|
|
392
400
|
var JsonLdContextDefinitionSchema = {
|
|
393
401
|
type: type$d,
|
|
394
402
|
properties: properties$6,
|
|
395
403
|
additionalProperties: additionalProperties$b,
|
|
404
|
+
description: description$n
|
|
405
|
+
};
|
|
406
|
+
|
|
407
|
+
var anyOf$9 = [
|
|
408
|
+
{
|
|
409
|
+
type: "null"
|
|
410
|
+
},
|
|
411
|
+
{
|
|
412
|
+
type: "string"
|
|
413
|
+
},
|
|
414
|
+
{
|
|
415
|
+
$ref: "https://schema.twindev.org/json-ld/JsonLdContextDefinition"
|
|
416
|
+
}
|
|
417
|
+
];
|
|
418
|
+
var description$m = "A context definition element is used to define the types of a context definition.";
|
|
419
|
+
var JsonLdContextDefinitionElementSchema = {
|
|
420
|
+
anyOf: anyOf$9,
|
|
421
|
+
description: description$m
|
|
422
|
+
};
|
|
423
|
+
|
|
424
|
+
var anyOf$8 = [
|
|
425
|
+
{
|
|
426
|
+
$ref: "https://schema.twindev.org/json-ld/JsonLdContextDefinitionElement"
|
|
427
|
+
},
|
|
428
|
+
{
|
|
429
|
+
type: "array",
|
|
430
|
+
items: {
|
|
431
|
+
$ref: "https://schema.twindev.org/json-ld/JsonLdContextDefinitionElement"
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
];
|
|
435
|
+
var description$l = "A context definition root is used to define the root of a context definition.";
|
|
436
|
+
var JsonLdContextDefinitionRootSchema = {
|
|
437
|
+
anyOf: anyOf$8,
|
|
396
438
|
description: description$l
|
|
397
439
|
};
|
|
398
440
|
|
|
@@ -1382,145 +1424,143 @@ class JsonLdDataTypes {
|
|
|
1382
1424
|
*/
|
|
1383
1425
|
static registerTypes() {
|
|
1384
1426
|
dataCore.DataTypeHandlerFactory.register(`${JsonLdContexts.ContextRoot}${JsonLdTypes.Document}`, () => ({
|
|
1427
|
+
context: JsonLdContexts.ContextRoot,
|
|
1385
1428
|
type: JsonLdTypes.Document,
|
|
1386
1429
|
jsonSchema: async () => JsonLdDocumentSchema
|
|
1387
1430
|
}));
|
|
1388
1431
|
dataCore.DataTypeHandlerFactory.register(`${JsonLdContexts.ContextRoot}${JsonLdTypes.Object}`, () => ({
|
|
1432
|
+
context: JsonLdContexts.ContextRoot,
|
|
1389
1433
|
type: JsonLdTypes.Object,
|
|
1390
1434
|
jsonSchema: async () => JsonLdObjectSchema
|
|
1391
1435
|
}));
|
|
1392
1436
|
dataCore.DataTypeHandlerFactory.register(`${JsonLdContexts.ContextRoot}${JsonLdTypes.NodeObject}`, () => ({
|
|
1437
|
+
context: JsonLdContexts.ContextRoot,
|
|
1393
1438
|
type: JsonLdTypes.NodeObject,
|
|
1394
1439
|
jsonSchema: async () => JsonLdNodeObjectSchema
|
|
1395
1440
|
}));
|
|
1396
1441
|
dataCore.DataTypeHandlerFactory.register(`${JsonLdContexts.ContextRoot}${JsonLdTypes.NodePrimitive}`, () => ({
|
|
1442
|
+
context: JsonLdContexts.ContextRoot,
|
|
1397
1443
|
type: JsonLdTypes.NodePrimitive,
|
|
1398
1444
|
jsonSchema: async () => JsonLdNodePrimitiveSchema
|
|
1399
1445
|
}));
|
|
1400
1446
|
dataCore.DataTypeHandlerFactory.register(`${JsonLdContexts.ContextRoot}${JsonLdTypes.GraphObject}`, () => ({
|
|
1447
|
+
context: JsonLdContexts.ContextRoot,
|
|
1401
1448
|
type: JsonLdTypes.GraphObject,
|
|
1402
1449
|
jsonSchema: async () => JsonLdGraphObjectSchema
|
|
1403
1450
|
}));
|
|
1404
1451
|
dataCore.DataTypeHandlerFactory.register(`${JsonLdContexts.ContextRoot}${JsonLdTypes.ValueObject}`, () => ({
|
|
1452
|
+
context: JsonLdContexts.ContextRoot,
|
|
1405
1453
|
type: JsonLdTypes.ValueObject,
|
|
1406
1454
|
jsonSchema: async () => JsonLdValueObjectSchema
|
|
1407
1455
|
}));
|
|
1408
1456
|
dataCore.DataTypeHandlerFactory.register(`${JsonLdContexts.ContextRoot}${JsonLdTypes.ListObject}`, () => ({
|
|
1457
|
+
context: JsonLdContexts.ContextRoot,
|
|
1409
1458
|
type: JsonLdTypes.ListObject,
|
|
1410
1459
|
jsonSchema: async () => JsonLdListObjectSchema
|
|
1411
1460
|
}));
|
|
1412
1461
|
dataCore.DataTypeHandlerFactory.register(`${JsonLdContexts.ContextRoot}${JsonLdTypes.ListObject}`, () => ({
|
|
1462
|
+
context: JsonLdContexts.ContextRoot,
|
|
1413
1463
|
type: JsonLdTypes.ListObject,
|
|
1414
1464
|
jsonSchema: async () => JsonLdListObjectSchema
|
|
1415
1465
|
}));
|
|
1416
1466
|
dataCore.DataTypeHandlerFactory.register(`${JsonLdContexts.ContextRoot}${JsonLdTypes.SetObject}`, () => ({
|
|
1467
|
+
context: JsonLdContexts.ContextRoot,
|
|
1417
1468
|
type: JsonLdTypes.SetObject,
|
|
1418
1469
|
jsonSchema: async () => JsonLdSetObjectSchema
|
|
1419
1470
|
}));
|
|
1420
1471
|
dataCore.DataTypeHandlerFactory.register(`${JsonLdContexts.ContextRoot}${JsonLdTypes.LanguageMap}`, () => ({
|
|
1472
|
+
context: JsonLdContexts.ContextRoot,
|
|
1421
1473
|
type: JsonLdTypes.LanguageMap,
|
|
1422
1474
|
jsonSchema: async () => JsonLdLanguageMapSchema
|
|
1423
1475
|
}));
|
|
1424
1476
|
dataCore.DataTypeHandlerFactory.register(`${JsonLdContexts.ContextRoot}${JsonLdTypes.IndexMap}`, () => ({
|
|
1477
|
+
context: JsonLdContexts.ContextRoot,
|
|
1425
1478
|
type: JsonLdTypes.IndexMap,
|
|
1426
1479
|
jsonSchema: async () => JsonLdIndexMapSchema
|
|
1427
1480
|
}));
|
|
1428
1481
|
dataCore.DataTypeHandlerFactory.register(`${JsonLdContexts.ContextRoot}${JsonLdTypes.IndexMapItem}`, () => ({
|
|
1482
|
+
context: JsonLdContexts.ContextRoot,
|
|
1429
1483
|
type: JsonLdTypes.IndexMapItem,
|
|
1430
1484
|
jsonSchema: async () => JsonLdIndexMapItemSchema
|
|
1431
1485
|
}));
|
|
1432
1486
|
dataCore.DataTypeHandlerFactory.register(`${JsonLdContexts.ContextRoot}${JsonLdTypes.IdMap}`, () => ({
|
|
1487
|
+
context: JsonLdContexts.ContextRoot,
|
|
1433
1488
|
type: JsonLdTypes.IdMap,
|
|
1434
1489
|
jsonSchema: async () => JsonLdIdMapSchema
|
|
1435
1490
|
}));
|
|
1436
1491
|
dataCore.DataTypeHandlerFactory.register(`${JsonLdContexts.ContextRoot}${JsonLdTypes.TypeMap}`, () => ({
|
|
1492
|
+
context: JsonLdContexts.ContextRoot,
|
|
1437
1493
|
type: JsonLdTypes.TypeMap,
|
|
1438
1494
|
jsonSchema: async () => JsonLdTypeMapSchema
|
|
1439
1495
|
}));
|
|
1440
1496
|
dataCore.DataTypeHandlerFactory.register(`${JsonLdContexts.ContextRoot}${JsonLdTypes.IncludedBlock}`, () => ({
|
|
1497
|
+
context: JsonLdContexts.ContextRoot,
|
|
1441
1498
|
type: JsonLdTypes.IncludedBlock,
|
|
1442
1499
|
jsonSchema: async () => JsonLdIncludedBlockSchema
|
|
1443
1500
|
}));
|
|
1444
1501
|
dataCore.DataTypeHandlerFactory.register(`${JsonLdContexts.ContextRoot}${JsonLdTypes.ContextDefinition}`, () => ({
|
|
1502
|
+
context: JsonLdContexts.ContextRoot,
|
|
1445
1503
|
type: JsonLdTypes.ContextDefinition,
|
|
1446
1504
|
jsonSchema: async () => JsonLdContextDefinitionSchema
|
|
1447
1505
|
}));
|
|
1506
|
+
dataCore.DataTypeHandlerFactory.register(`${JsonLdContexts.ContextRoot}${JsonLdTypes.ContextDefinitionElement}`, () => ({
|
|
1507
|
+
context: JsonLdContexts.ContextRoot,
|
|
1508
|
+
type: JsonLdTypes.ContextDefinitionElement,
|
|
1509
|
+
jsonSchema: async () => JsonLdContextDefinitionElementSchema
|
|
1510
|
+
}));
|
|
1511
|
+
dataCore.DataTypeHandlerFactory.register(`${JsonLdContexts.ContextRoot}${JsonLdTypes.ContextDefinitionRoot}`, () => ({
|
|
1512
|
+
context: JsonLdContexts.ContextRoot,
|
|
1513
|
+
type: JsonLdTypes.ContextDefinitionRoot,
|
|
1514
|
+
jsonSchema: async () => JsonLdContextDefinitionRootSchema
|
|
1515
|
+
}));
|
|
1448
1516
|
dataCore.DataTypeHandlerFactory.register(`${JsonLdContexts.ContextRoot}${JsonLdTypes.ExpandedTermDefinition}`, () => ({
|
|
1517
|
+
context: JsonLdContexts.ContextRoot,
|
|
1449
1518
|
type: JsonLdTypes.ExpandedTermDefinition,
|
|
1450
1519
|
jsonSchema: async () => JsonLdExpandedTermDefinitionSchema
|
|
1451
1520
|
}));
|
|
1452
1521
|
dataCore.DataTypeHandlerFactory.register(`${JsonLdContexts.ContextRoot}${JsonLdTypes.Keyword}`, () => ({
|
|
1522
|
+
context: JsonLdContexts.ContextRoot,
|
|
1453
1523
|
type: JsonLdTypes.Keyword,
|
|
1454
1524
|
jsonSchema: async () => JsonLdKeywordSchema
|
|
1455
1525
|
}));
|
|
1456
1526
|
dataCore.DataTypeHandlerFactory.register(`${JsonLdContexts.ContextRoot}${JsonLdTypes.ListOrSetItem}`, () => ({
|
|
1527
|
+
context: JsonLdContexts.ContextRoot,
|
|
1457
1528
|
type: JsonLdTypes.ListOrSetItem,
|
|
1458
1529
|
jsonSchema: async () => JsonLdListOrSetItemSchema
|
|
1459
1530
|
}));
|
|
1460
1531
|
dataCore.DataTypeHandlerFactory.register(`${JsonLdContexts.ContextRoot}${JsonLdTypes.ContainerType}`, () => ({
|
|
1532
|
+
context: JsonLdContexts.ContextRoot,
|
|
1461
1533
|
type: JsonLdTypes.ContainerType,
|
|
1462
1534
|
jsonSchema: async () => JsonLdContainerTypeSchema
|
|
1463
1535
|
}));
|
|
1464
1536
|
dataCore.DataTypeHandlerFactory.register(`${JsonLdContexts.ContextRoot}${JsonLdTypes.ContainerTypeArray}`, () => ({
|
|
1537
|
+
context: JsonLdContexts.ContextRoot,
|
|
1465
1538
|
type: JsonLdTypes.ContainerTypeArray,
|
|
1466
1539
|
jsonSchema: async () => JsonLdContainerTypeArraySchema
|
|
1467
1540
|
}));
|
|
1468
1541
|
dataCore.DataTypeHandlerFactory.register(`${JsonLdContexts.ContextRoot}${JsonLdTypes.JsonPrimitive}`, () => ({
|
|
1542
|
+
context: JsonLdContexts.ContextRoot,
|
|
1469
1543
|
type: JsonLdTypes.JsonPrimitive,
|
|
1470
1544
|
jsonSchema: async () => JsonLdJsonPrimitiveSchema
|
|
1471
1545
|
}));
|
|
1472
1546
|
dataCore.DataTypeHandlerFactory.register(`${JsonLdContexts.ContextRoot}${JsonLdTypes.JsonArray}`, () => ({
|
|
1547
|
+
context: JsonLdContexts.ContextRoot,
|
|
1473
1548
|
type: JsonLdTypes.JsonArray,
|
|
1474
1549
|
jsonSchema: async () => JsonLdJsonArraySchema
|
|
1475
1550
|
}));
|
|
1476
1551
|
dataCore.DataTypeHandlerFactory.register(`${JsonLdContexts.ContextRoot}${JsonLdTypes.JsonObject}`, () => ({
|
|
1552
|
+
context: JsonLdContexts.ContextRoot,
|
|
1477
1553
|
type: JsonLdTypes.JsonObject,
|
|
1478
1554
|
jsonSchema: async () => JsonLdJsonObjectSchema
|
|
1479
1555
|
}));
|
|
1480
1556
|
dataCore.DataTypeHandlerFactory.register(`${JsonLdContexts.ContextRoot}${JsonLdTypes.JsonValue}`, () => ({
|
|
1557
|
+
context: JsonLdContexts.ContextRoot,
|
|
1481
1558
|
type: JsonLdTypes.JsonValue,
|
|
1482
1559
|
jsonSchema: async () => JsonLdJsonValueSchema
|
|
1483
1560
|
}));
|
|
1484
1561
|
}
|
|
1485
1562
|
}
|
|
1486
1563
|
|
|
1487
|
-
// Copyright 2024 IOTA Stiftung.
|
|
1488
|
-
// SPDX-License-Identifier: Apache-2.0.
|
|
1489
|
-
/**
|
|
1490
|
-
* Class to help with JSON LD.
|
|
1491
|
-
*/
|
|
1492
|
-
class JsonLdHelper {
|
|
1493
|
-
/**
|
|
1494
|
-
* Validate a JSON-LD document.
|
|
1495
|
-
* @param document The JSON-LD document to validate.
|
|
1496
|
-
* @param validationFailures The list of validation failures to add to.
|
|
1497
|
-
* @param validationMode The validation mode to use, defaults to either.
|
|
1498
|
-
* @returns True if the document was valid.
|
|
1499
|
-
*/
|
|
1500
|
-
static async validate(document, validationFailures, validationMode) {
|
|
1501
|
-
if (core.Is.array(document)) {
|
|
1502
|
-
// If the document is an array of nodes, validate each node
|
|
1503
|
-
for (const node of document) {
|
|
1504
|
-
await JsonLdHelper.validate(node, validationFailures, validationMode);
|
|
1505
|
-
}
|
|
1506
|
-
}
|
|
1507
|
-
else if (core.Is.array(document["@graph"])) {
|
|
1508
|
-
// If the graph is an array of nodes, validate each node
|
|
1509
|
-
for (const node of document["@graph"]) {
|
|
1510
|
-
await JsonLdHelper.validate(node, validationFailures, validationMode);
|
|
1511
|
-
}
|
|
1512
|
-
}
|
|
1513
|
-
else if (core.Is.object(document)) {
|
|
1514
|
-
// If the graph is a single node, then use the validate the node schema
|
|
1515
|
-
const dataType = document["@type"];
|
|
1516
|
-
if (core.Is.stringValue(dataType)) {
|
|
1517
|
-
await dataCore.DataTypeHelper.validate("document", dataType, document, validationFailures, validationMode);
|
|
1518
|
-
}
|
|
1519
|
-
}
|
|
1520
|
-
return validationFailures.length === 0;
|
|
1521
|
-
}
|
|
1522
|
-
}
|
|
1523
|
-
|
|
1524
1564
|
// Copyright 2024 IOTA Stiftung.
|
|
1525
1565
|
// SPDX-License-Identifier: Apache-2.0.
|
|
1526
1566
|
/**
|
|
@@ -1591,31 +1631,48 @@ class JsonLdProcessor {
|
|
|
1591
1631
|
/**
|
|
1592
1632
|
* Compact a document according to a particular context.
|
|
1593
1633
|
* @param document The JSON-LD document to compact.
|
|
1594
|
-
* @param context The context to compact the document to, if not provided will
|
|
1634
|
+
* @param context The context to compact the document to, if not provided will use the one in the document.
|
|
1635
|
+
* @param options The options for compacting the document.
|
|
1636
|
+
* @param options.itemListOverride Whether to override the itemListElement context with a set, defaults to true.
|
|
1595
1637
|
* @returns The compacted JSON-LD document.
|
|
1596
1638
|
*/
|
|
1597
|
-
static async compact(document, context) {
|
|
1639
|
+
static async compact(document, context, options) {
|
|
1598
1640
|
try {
|
|
1599
1641
|
if (core.Is.object(document)) {
|
|
1600
|
-
|
|
1601
|
-
|
|
1602
|
-
|
|
1603
|
-
|
|
1604
|
-
|
|
1605
|
-
|
|
1606
|
-
|
|
1607
|
-
|
|
1608
|
-
|
|
1609
|
-
|
|
1642
|
+
// If the user didn't provide a context, use the one from the document
|
|
1643
|
+
if (core.Is.empty(context) && !core.Is.empty(document["@context"])) {
|
|
1644
|
+
context = document["@context"];
|
|
1645
|
+
}
|
|
1646
|
+
const overrideListElementOption = options?.itemListOverride ?? true;
|
|
1647
|
+
let overrideContext;
|
|
1648
|
+
if (overrideListElementOption) {
|
|
1649
|
+
// The compactArrays flag doesn't work with the current version of jsonld.js
|
|
1650
|
+
// For list results we standardise on ItemList and itemListElement
|
|
1651
|
+
// so we modify the schema.org type for itemListElement to be a set which bypasses the issue
|
|
1652
|
+
// https://github.com/digitalbazaar/jsonld.js/issues/247
|
|
1653
|
+
overrideContext = {
|
|
1654
|
+
itemListElement: {
|
|
1655
|
+
"@id": "http://schema.org/itemListElement",
|
|
1656
|
+
"@container": "@set",
|
|
1657
|
+
"@protected": true
|
|
1610
1658
|
}
|
|
1659
|
+
};
|
|
1660
|
+
if (core.Is.object(context) && "@context" in context) {
|
|
1661
|
+
// If the context is an object, we need to merge it with the override context
|
|
1662
|
+
context = JsonLdProcessor.combineContexts(context["@context"], overrideContext);
|
|
1611
1663
|
}
|
|
1612
|
-
else
|
|
1613
|
-
context
|
|
1664
|
+
else {
|
|
1665
|
+
// If the context is a string or an array, we need to merge it with the override context
|
|
1666
|
+
context = JsonLdProcessor.combineContexts(context, overrideContext);
|
|
1614
1667
|
}
|
|
1615
1668
|
}
|
|
1616
1669
|
const compacted = await jsonLd.compact(core.ObjectHelper.removeEmptyProperties(document), context, {
|
|
1617
1670
|
documentLoader: JsonLdProcessor.getDocumentLoader()
|
|
1618
1671
|
});
|
|
1672
|
+
if (!core.Is.empty(overrideContext)) {
|
|
1673
|
+
// Remove the override context from the compacted document
|
|
1674
|
+
compacted["@context"] = JsonLdProcessor.removeContexts(compacted["@context"], [overrideContext]);
|
|
1675
|
+
}
|
|
1619
1676
|
return compacted;
|
|
1620
1677
|
}
|
|
1621
1678
|
return document;
|
|
@@ -1829,17 +1886,34 @@ class JsonLdProcessor {
|
|
|
1829
1886
|
break;
|
|
1830
1887
|
}
|
|
1831
1888
|
}
|
|
1832
|
-
|
|
1833
|
-
|
|
1834
|
-
|
|
1835
|
-
|
|
1836
|
-
|
|
1889
|
+
try {
|
|
1890
|
+
const response = await web.FetchHelper.fetchJson(JsonLdProcessor._CLASS_NAME, url, web.HttpMethod.GET, undefined, {
|
|
1891
|
+
cacheTtlMs: JsonLdProcessor.getCacheLimit(),
|
|
1892
|
+
headers: {
|
|
1893
|
+
[web.HeaderTypes.Accept]: web.MimeTypes.JsonLd
|
|
1894
|
+
}
|
|
1895
|
+
});
|
|
1896
|
+
return {
|
|
1897
|
+
documentUrl: url,
|
|
1898
|
+
document: response
|
|
1899
|
+
};
|
|
1900
|
+
}
|
|
1901
|
+
catch (err) {
|
|
1902
|
+
const error = core.BaseError.fromError(err);
|
|
1903
|
+
if (error.message.includes("is not valid JSON")) {
|
|
1904
|
+
const response = await web.FetchHelper.fetchJson(JsonLdProcessor._CLASS_NAME, url, web.HttpMethod.GET, undefined, {
|
|
1905
|
+
cacheTtlMs: JsonLdProcessor.getCacheLimit(),
|
|
1906
|
+
headers: {
|
|
1907
|
+
[web.HeaderTypes.Accept]: web.MimeTypes.Json
|
|
1908
|
+
}
|
|
1909
|
+
});
|
|
1910
|
+
return {
|
|
1911
|
+
documentUrl: url,
|
|
1912
|
+
document: response
|
|
1913
|
+
};
|
|
1837
1914
|
}
|
|
1838
|
-
|
|
1839
|
-
|
|
1840
|
-
documentUrl: url,
|
|
1841
|
-
document: response
|
|
1842
|
-
};
|
|
1915
|
+
throw err;
|
|
1916
|
+
}
|
|
1843
1917
|
}
|
|
1844
1918
|
/**
|
|
1845
1919
|
* Handle common errors.
|
|
@@ -1858,6 +1932,45 @@ class JsonLdProcessor {
|
|
|
1858
1932
|
}
|
|
1859
1933
|
}
|
|
1860
1934
|
|
|
1935
|
+
// Copyright 2024 IOTA Stiftung.
|
|
1936
|
+
// SPDX-License-Identifier: Apache-2.0.
|
|
1937
|
+
/**
|
|
1938
|
+
* Class to help with JSON LD.
|
|
1939
|
+
*/
|
|
1940
|
+
class JsonLdHelper {
|
|
1941
|
+
/**
|
|
1942
|
+
* Validate a JSON-LD document.
|
|
1943
|
+
* @param document The JSON-LD document to validate.
|
|
1944
|
+
* @param validationFailures The list of validation failures to add to.
|
|
1945
|
+
* @param validationMode The validation mode to use, defaults to either.
|
|
1946
|
+
* @returns True if the document was valid.
|
|
1947
|
+
*/
|
|
1948
|
+
static async validate(document, validationFailures, validationMode) {
|
|
1949
|
+
if (core.Is.array(document)) {
|
|
1950
|
+
// If the document is an array of nodes, validate each node
|
|
1951
|
+
for (const node of document) {
|
|
1952
|
+
await JsonLdHelper.validate(node, validationFailures, validationMode);
|
|
1953
|
+
}
|
|
1954
|
+
}
|
|
1955
|
+
else if (core.Is.array(document["@graph"])) {
|
|
1956
|
+
// If the graph is an array of nodes, validate each node
|
|
1957
|
+
for (const node of document["@graph"]) {
|
|
1958
|
+
await JsonLdHelper.validate(node, validationFailures, validationMode);
|
|
1959
|
+
}
|
|
1960
|
+
}
|
|
1961
|
+
else if (core.Is.object(document)) {
|
|
1962
|
+
// Expand the document to ensure we have the full context for types
|
|
1963
|
+
// As the data types in the factories are not always fully qualified
|
|
1964
|
+
const expandedDoc = await JsonLdProcessor.expand(document);
|
|
1965
|
+
const expandedDataType = core.ArrayHelper.fromObjectOrArray(expandedDoc[0]["@type"]);
|
|
1966
|
+
if (core.Is.arrayValue(expandedDataType)) {
|
|
1967
|
+
await dataCore.DataTypeHelper.validate("document", expandedDataType[0], document, validationFailures, validationMode);
|
|
1968
|
+
}
|
|
1969
|
+
}
|
|
1970
|
+
return validationFailures.length === 0;
|
|
1971
|
+
}
|
|
1972
|
+
}
|
|
1973
|
+
|
|
1861
1974
|
exports.JsonLdContexts = JsonLdContexts;
|
|
1862
1975
|
exports.JsonLdDataTypes = JsonLdDataTypes;
|
|
1863
1976
|
exports.JsonLdHelper = JsonLdHelper;
|
package/dist/esm/index.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { DataTypeHandlerFactory, DataTypeHelper } from '@twin.org/data-core';
|
|
2
|
-
import {
|
|
3
|
-
import { FetchHelper, HttpMethod,
|
|
2
|
+
import { SharedStore, Is, ObjectHelper, GeneralError, BaseError, ArrayHelper } from '@twin.org/core';
|
|
3
|
+
import { FetchHelper, HttpMethod, HeaderTypes, MimeTypes } from '@twin.org/web';
|
|
4
4
|
import jsonLd from 'jsonld';
|
|
5
5
|
|
|
6
6
|
// Copyright 2024 IOTA Stiftung.
|
|
@@ -83,6 +83,14 @@ const JsonLdTypes = {
|
|
|
83
83
|
* Represents JSON-LD Context Definition.
|
|
84
84
|
*/
|
|
85
85
|
ContextDefinition: "JsonLdContextDefinition",
|
|
86
|
+
/**
|
|
87
|
+
* Represents JSON-LD Context Definition Element.
|
|
88
|
+
*/
|
|
89
|
+
ContextDefinitionElement: "JsonLdContextDefinitionElement",
|
|
90
|
+
/**
|
|
91
|
+
* Represents JSON-LD Context Definition Root.
|
|
92
|
+
*/
|
|
93
|
+
ContextDefinitionRoot: "JsonLdContextDefinitionRoot",
|
|
86
94
|
/**
|
|
87
95
|
* Represents JSON-LD Expanded Term Definition.
|
|
88
96
|
*/
|
|
@@ -122,7 +130,7 @@ const JsonLdTypes = {
|
|
|
122
130
|
};
|
|
123
131
|
|
|
124
132
|
var type$e = "string";
|
|
125
|
-
var description$
|
|
133
|
+
var description$p = "Helper Types";
|
|
126
134
|
var JsonLdContainerTypeSchema = {
|
|
127
135
|
type: type$e,
|
|
128
136
|
"enum": [
|
|
@@ -132,10 +140,10 @@ var JsonLdContainerTypeSchema = {
|
|
|
132
140
|
"@graph",
|
|
133
141
|
"@type"
|
|
134
142
|
],
|
|
135
|
-
description: description$
|
|
143
|
+
description: description$p
|
|
136
144
|
};
|
|
137
145
|
|
|
138
|
-
var anyOf$
|
|
146
|
+
var anyOf$a = [
|
|
139
147
|
{
|
|
140
148
|
type: "array",
|
|
141
149
|
minItems: 2,
|
|
@@ -309,10 +317,10 @@ var anyOf$8 = [
|
|
|
309
317
|
maxItems: 2
|
|
310
318
|
}
|
|
311
319
|
];
|
|
312
|
-
var description$
|
|
320
|
+
var description$o = "Helper Types.";
|
|
313
321
|
var JsonLdContainerTypeArraySchema = {
|
|
314
|
-
anyOf: anyOf$
|
|
315
|
-
description: description$
|
|
322
|
+
anyOf: anyOf$a,
|
|
323
|
+
description: description$o
|
|
316
324
|
};
|
|
317
325
|
|
|
318
326
|
var type$d = "object";
|
|
@@ -386,11 +394,45 @@ var additionalProperties$b = {
|
|
|
386
394
|
}
|
|
387
395
|
]
|
|
388
396
|
};
|
|
389
|
-
var description$
|
|
397
|
+
var description$n = "A context definition defines a local context in a node object.";
|
|
390
398
|
var JsonLdContextDefinitionSchema = {
|
|
391
399
|
type: type$d,
|
|
392
400
|
properties: properties$6,
|
|
393
401
|
additionalProperties: additionalProperties$b,
|
|
402
|
+
description: description$n
|
|
403
|
+
};
|
|
404
|
+
|
|
405
|
+
var anyOf$9 = [
|
|
406
|
+
{
|
|
407
|
+
type: "null"
|
|
408
|
+
},
|
|
409
|
+
{
|
|
410
|
+
type: "string"
|
|
411
|
+
},
|
|
412
|
+
{
|
|
413
|
+
$ref: "https://schema.twindev.org/json-ld/JsonLdContextDefinition"
|
|
414
|
+
}
|
|
415
|
+
];
|
|
416
|
+
var description$m = "A context definition element is used to define the types of a context definition.";
|
|
417
|
+
var JsonLdContextDefinitionElementSchema = {
|
|
418
|
+
anyOf: anyOf$9,
|
|
419
|
+
description: description$m
|
|
420
|
+
};
|
|
421
|
+
|
|
422
|
+
var anyOf$8 = [
|
|
423
|
+
{
|
|
424
|
+
$ref: "https://schema.twindev.org/json-ld/JsonLdContextDefinitionElement"
|
|
425
|
+
},
|
|
426
|
+
{
|
|
427
|
+
type: "array",
|
|
428
|
+
items: {
|
|
429
|
+
$ref: "https://schema.twindev.org/json-ld/JsonLdContextDefinitionElement"
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
];
|
|
433
|
+
var description$l = "A context definition root is used to define the root of a context definition.";
|
|
434
|
+
var JsonLdContextDefinitionRootSchema = {
|
|
435
|
+
anyOf: anyOf$8,
|
|
394
436
|
description: description$l
|
|
395
437
|
};
|
|
396
438
|
|
|
@@ -1380,145 +1422,143 @@ class JsonLdDataTypes {
|
|
|
1380
1422
|
*/
|
|
1381
1423
|
static registerTypes() {
|
|
1382
1424
|
DataTypeHandlerFactory.register(`${JsonLdContexts.ContextRoot}${JsonLdTypes.Document}`, () => ({
|
|
1425
|
+
context: JsonLdContexts.ContextRoot,
|
|
1383
1426
|
type: JsonLdTypes.Document,
|
|
1384
1427
|
jsonSchema: async () => JsonLdDocumentSchema
|
|
1385
1428
|
}));
|
|
1386
1429
|
DataTypeHandlerFactory.register(`${JsonLdContexts.ContextRoot}${JsonLdTypes.Object}`, () => ({
|
|
1430
|
+
context: JsonLdContexts.ContextRoot,
|
|
1387
1431
|
type: JsonLdTypes.Object,
|
|
1388
1432
|
jsonSchema: async () => JsonLdObjectSchema
|
|
1389
1433
|
}));
|
|
1390
1434
|
DataTypeHandlerFactory.register(`${JsonLdContexts.ContextRoot}${JsonLdTypes.NodeObject}`, () => ({
|
|
1435
|
+
context: JsonLdContexts.ContextRoot,
|
|
1391
1436
|
type: JsonLdTypes.NodeObject,
|
|
1392
1437
|
jsonSchema: async () => JsonLdNodeObjectSchema
|
|
1393
1438
|
}));
|
|
1394
1439
|
DataTypeHandlerFactory.register(`${JsonLdContexts.ContextRoot}${JsonLdTypes.NodePrimitive}`, () => ({
|
|
1440
|
+
context: JsonLdContexts.ContextRoot,
|
|
1395
1441
|
type: JsonLdTypes.NodePrimitive,
|
|
1396
1442
|
jsonSchema: async () => JsonLdNodePrimitiveSchema
|
|
1397
1443
|
}));
|
|
1398
1444
|
DataTypeHandlerFactory.register(`${JsonLdContexts.ContextRoot}${JsonLdTypes.GraphObject}`, () => ({
|
|
1445
|
+
context: JsonLdContexts.ContextRoot,
|
|
1399
1446
|
type: JsonLdTypes.GraphObject,
|
|
1400
1447
|
jsonSchema: async () => JsonLdGraphObjectSchema
|
|
1401
1448
|
}));
|
|
1402
1449
|
DataTypeHandlerFactory.register(`${JsonLdContexts.ContextRoot}${JsonLdTypes.ValueObject}`, () => ({
|
|
1450
|
+
context: JsonLdContexts.ContextRoot,
|
|
1403
1451
|
type: JsonLdTypes.ValueObject,
|
|
1404
1452
|
jsonSchema: async () => JsonLdValueObjectSchema
|
|
1405
1453
|
}));
|
|
1406
1454
|
DataTypeHandlerFactory.register(`${JsonLdContexts.ContextRoot}${JsonLdTypes.ListObject}`, () => ({
|
|
1455
|
+
context: JsonLdContexts.ContextRoot,
|
|
1407
1456
|
type: JsonLdTypes.ListObject,
|
|
1408
1457
|
jsonSchema: async () => JsonLdListObjectSchema
|
|
1409
1458
|
}));
|
|
1410
1459
|
DataTypeHandlerFactory.register(`${JsonLdContexts.ContextRoot}${JsonLdTypes.ListObject}`, () => ({
|
|
1460
|
+
context: JsonLdContexts.ContextRoot,
|
|
1411
1461
|
type: JsonLdTypes.ListObject,
|
|
1412
1462
|
jsonSchema: async () => JsonLdListObjectSchema
|
|
1413
1463
|
}));
|
|
1414
1464
|
DataTypeHandlerFactory.register(`${JsonLdContexts.ContextRoot}${JsonLdTypes.SetObject}`, () => ({
|
|
1465
|
+
context: JsonLdContexts.ContextRoot,
|
|
1415
1466
|
type: JsonLdTypes.SetObject,
|
|
1416
1467
|
jsonSchema: async () => JsonLdSetObjectSchema
|
|
1417
1468
|
}));
|
|
1418
1469
|
DataTypeHandlerFactory.register(`${JsonLdContexts.ContextRoot}${JsonLdTypes.LanguageMap}`, () => ({
|
|
1470
|
+
context: JsonLdContexts.ContextRoot,
|
|
1419
1471
|
type: JsonLdTypes.LanguageMap,
|
|
1420
1472
|
jsonSchema: async () => JsonLdLanguageMapSchema
|
|
1421
1473
|
}));
|
|
1422
1474
|
DataTypeHandlerFactory.register(`${JsonLdContexts.ContextRoot}${JsonLdTypes.IndexMap}`, () => ({
|
|
1475
|
+
context: JsonLdContexts.ContextRoot,
|
|
1423
1476
|
type: JsonLdTypes.IndexMap,
|
|
1424
1477
|
jsonSchema: async () => JsonLdIndexMapSchema
|
|
1425
1478
|
}));
|
|
1426
1479
|
DataTypeHandlerFactory.register(`${JsonLdContexts.ContextRoot}${JsonLdTypes.IndexMapItem}`, () => ({
|
|
1480
|
+
context: JsonLdContexts.ContextRoot,
|
|
1427
1481
|
type: JsonLdTypes.IndexMapItem,
|
|
1428
1482
|
jsonSchema: async () => JsonLdIndexMapItemSchema
|
|
1429
1483
|
}));
|
|
1430
1484
|
DataTypeHandlerFactory.register(`${JsonLdContexts.ContextRoot}${JsonLdTypes.IdMap}`, () => ({
|
|
1485
|
+
context: JsonLdContexts.ContextRoot,
|
|
1431
1486
|
type: JsonLdTypes.IdMap,
|
|
1432
1487
|
jsonSchema: async () => JsonLdIdMapSchema
|
|
1433
1488
|
}));
|
|
1434
1489
|
DataTypeHandlerFactory.register(`${JsonLdContexts.ContextRoot}${JsonLdTypes.TypeMap}`, () => ({
|
|
1490
|
+
context: JsonLdContexts.ContextRoot,
|
|
1435
1491
|
type: JsonLdTypes.TypeMap,
|
|
1436
1492
|
jsonSchema: async () => JsonLdTypeMapSchema
|
|
1437
1493
|
}));
|
|
1438
1494
|
DataTypeHandlerFactory.register(`${JsonLdContexts.ContextRoot}${JsonLdTypes.IncludedBlock}`, () => ({
|
|
1495
|
+
context: JsonLdContexts.ContextRoot,
|
|
1439
1496
|
type: JsonLdTypes.IncludedBlock,
|
|
1440
1497
|
jsonSchema: async () => JsonLdIncludedBlockSchema
|
|
1441
1498
|
}));
|
|
1442
1499
|
DataTypeHandlerFactory.register(`${JsonLdContexts.ContextRoot}${JsonLdTypes.ContextDefinition}`, () => ({
|
|
1500
|
+
context: JsonLdContexts.ContextRoot,
|
|
1443
1501
|
type: JsonLdTypes.ContextDefinition,
|
|
1444
1502
|
jsonSchema: async () => JsonLdContextDefinitionSchema
|
|
1445
1503
|
}));
|
|
1504
|
+
DataTypeHandlerFactory.register(`${JsonLdContexts.ContextRoot}${JsonLdTypes.ContextDefinitionElement}`, () => ({
|
|
1505
|
+
context: JsonLdContexts.ContextRoot,
|
|
1506
|
+
type: JsonLdTypes.ContextDefinitionElement,
|
|
1507
|
+
jsonSchema: async () => JsonLdContextDefinitionElementSchema
|
|
1508
|
+
}));
|
|
1509
|
+
DataTypeHandlerFactory.register(`${JsonLdContexts.ContextRoot}${JsonLdTypes.ContextDefinitionRoot}`, () => ({
|
|
1510
|
+
context: JsonLdContexts.ContextRoot,
|
|
1511
|
+
type: JsonLdTypes.ContextDefinitionRoot,
|
|
1512
|
+
jsonSchema: async () => JsonLdContextDefinitionRootSchema
|
|
1513
|
+
}));
|
|
1446
1514
|
DataTypeHandlerFactory.register(`${JsonLdContexts.ContextRoot}${JsonLdTypes.ExpandedTermDefinition}`, () => ({
|
|
1515
|
+
context: JsonLdContexts.ContextRoot,
|
|
1447
1516
|
type: JsonLdTypes.ExpandedTermDefinition,
|
|
1448
1517
|
jsonSchema: async () => JsonLdExpandedTermDefinitionSchema
|
|
1449
1518
|
}));
|
|
1450
1519
|
DataTypeHandlerFactory.register(`${JsonLdContexts.ContextRoot}${JsonLdTypes.Keyword}`, () => ({
|
|
1520
|
+
context: JsonLdContexts.ContextRoot,
|
|
1451
1521
|
type: JsonLdTypes.Keyword,
|
|
1452
1522
|
jsonSchema: async () => JsonLdKeywordSchema
|
|
1453
1523
|
}));
|
|
1454
1524
|
DataTypeHandlerFactory.register(`${JsonLdContexts.ContextRoot}${JsonLdTypes.ListOrSetItem}`, () => ({
|
|
1525
|
+
context: JsonLdContexts.ContextRoot,
|
|
1455
1526
|
type: JsonLdTypes.ListOrSetItem,
|
|
1456
1527
|
jsonSchema: async () => JsonLdListOrSetItemSchema
|
|
1457
1528
|
}));
|
|
1458
1529
|
DataTypeHandlerFactory.register(`${JsonLdContexts.ContextRoot}${JsonLdTypes.ContainerType}`, () => ({
|
|
1530
|
+
context: JsonLdContexts.ContextRoot,
|
|
1459
1531
|
type: JsonLdTypes.ContainerType,
|
|
1460
1532
|
jsonSchema: async () => JsonLdContainerTypeSchema
|
|
1461
1533
|
}));
|
|
1462
1534
|
DataTypeHandlerFactory.register(`${JsonLdContexts.ContextRoot}${JsonLdTypes.ContainerTypeArray}`, () => ({
|
|
1535
|
+
context: JsonLdContexts.ContextRoot,
|
|
1463
1536
|
type: JsonLdTypes.ContainerTypeArray,
|
|
1464
1537
|
jsonSchema: async () => JsonLdContainerTypeArraySchema
|
|
1465
1538
|
}));
|
|
1466
1539
|
DataTypeHandlerFactory.register(`${JsonLdContexts.ContextRoot}${JsonLdTypes.JsonPrimitive}`, () => ({
|
|
1540
|
+
context: JsonLdContexts.ContextRoot,
|
|
1467
1541
|
type: JsonLdTypes.JsonPrimitive,
|
|
1468
1542
|
jsonSchema: async () => JsonLdJsonPrimitiveSchema
|
|
1469
1543
|
}));
|
|
1470
1544
|
DataTypeHandlerFactory.register(`${JsonLdContexts.ContextRoot}${JsonLdTypes.JsonArray}`, () => ({
|
|
1545
|
+
context: JsonLdContexts.ContextRoot,
|
|
1471
1546
|
type: JsonLdTypes.JsonArray,
|
|
1472
1547
|
jsonSchema: async () => JsonLdJsonArraySchema
|
|
1473
1548
|
}));
|
|
1474
1549
|
DataTypeHandlerFactory.register(`${JsonLdContexts.ContextRoot}${JsonLdTypes.JsonObject}`, () => ({
|
|
1550
|
+
context: JsonLdContexts.ContextRoot,
|
|
1475
1551
|
type: JsonLdTypes.JsonObject,
|
|
1476
1552
|
jsonSchema: async () => JsonLdJsonObjectSchema
|
|
1477
1553
|
}));
|
|
1478
1554
|
DataTypeHandlerFactory.register(`${JsonLdContexts.ContextRoot}${JsonLdTypes.JsonValue}`, () => ({
|
|
1555
|
+
context: JsonLdContexts.ContextRoot,
|
|
1479
1556
|
type: JsonLdTypes.JsonValue,
|
|
1480
1557
|
jsonSchema: async () => JsonLdJsonValueSchema
|
|
1481
1558
|
}));
|
|
1482
1559
|
}
|
|
1483
1560
|
}
|
|
1484
1561
|
|
|
1485
|
-
// Copyright 2024 IOTA Stiftung.
|
|
1486
|
-
// SPDX-License-Identifier: Apache-2.0.
|
|
1487
|
-
/**
|
|
1488
|
-
* Class to help with JSON LD.
|
|
1489
|
-
*/
|
|
1490
|
-
class JsonLdHelper {
|
|
1491
|
-
/**
|
|
1492
|
-
* Validate a JSON-LD document.
|
|
1493
|
-
* @param document The JSON-LD document to validate.
|
|
1494
|
-
* @param validationFailures The list of validation failures to add to.
|
|
1495
|
-
* @param validationMode The validation mode to use, defaults to either.
|
|
1496
|
-
* @returns True if the document was valid.
|
|
1497
|
-
*/
|
|
1498
|
-
static async validate(document, validationFailures, validationMode) {
|
|
1499
|
-
if (Is.array(document)) {
|
|
1500
|
-
// If the document is an array of nodes, validate each node
|
|
1501
|
-
for (const node of document) {
|
|
1502
|
-
await JsonLdHelper.validate(node, validationFailures, validationMode);
|
|
1503
|
-
}
|
|
1504
|
-
}
|
|
1505
|
-
else if (Is.array(document["@graph"])) {
|
|
1506
|
-
// If the graph is an array of nodes, validate each node
|
|
1507
|
-
for (const node of document["@graph"]) {
|
|
1508
|
-
await JsonLdHelper.validate(node, validationFailures, validationMode);
|
|
1509
|
-
}
|
|
1510
|
-
}
|
|
1511
|
-
else if (Is.object(document)) {
|
|
1512
|
-
// If the graph is a single node, then use the validate the node schema
|
|
1513
|
-
const dataType = document["@type"];
|
|
1514
|
-
if (Is.stringValue(dataType)) {
|
|
1515
|
-
await DataTypeHelper.validate("document", dataType, document, validationFailures, validationMode);
|
|
1516
|
-
}
|
|
1517
|
-
}
|
|
1518
|
-
return validationFailures.length === 0;
|
|
1519
|
-
}
|
|
1520
|
-
}
|
|
1521
|
-
|
|
1522
1562
|
// Copyright 2024 IOTA Stiftung.
|
|
1523
1563
|
// SPDX-License-Identifier: Apache-2.0.
|
|
1524
1564
|
/**
|
|
@@ -1589,31 +1629,48 @@ class JsonLdProcessor {
|
|
|
1589
1629
|
/**
|
|
1590
1630
|
* Compact a document according to a particular context.
|
|
1591
1631
|
* @param document The JSON-LD document to compact.
|
|
1592
|
-
* @param context The context to compact the document to, if not provided will
|
|
1632
|
+
* @param context The context to compact the document to, if not provided will use the one in the document.
|
|
1633
|
+
* @param options The options for compacting the document.
|
|
1634
|
+
* @param options.itemListOverride Whether to override the itemListElement context with a set, defaults to true.
|
|
1593
1635
|
* @returns The compacted JSON-LD document.
|
|
1594
1636
|
*/
|
|
1595
|
-
static async compact(document, context) {
|
|
1637
|
+
static async compact(document, context, options) {
|
|
1596
1638
|
try {
|
|
1597
1639
|
if (Is.object(document)) {
|
|
1598
|
-
|
|
1599
|
-
|
|
1600
|
-
|
|
1601
|
-
|
|
1602
|
-
|
|
1603
|
-
|
|
1604
|
-
|
|
1605
|
-
|
|
1606
|
-
|
|
1607
|
-
|
|
1640
|
+
// If the user didn't provide a context, use the one from the document
|
|
1641
|
+
if (Is.empty(context) && !Is.empty(document["@context"])) {
|
|
1642
|
+
context = document["@context"];
|
|
1643
|
+
}
|
|
1644
|
+
const overrideListElementOption = options?.itemListOverride ?? true;
|
|
1645
|
+
let overrideContext;
|
|
1646
|
+
if (overrideListElementOption) {
|
|
1647
|
+
// The compactArrays flag doesn't work with the current version of jsonld.js
|
|
1648
|
+
// For list results we standardise on ItemList and itemListElement
|
|
1649
|
+
// so we modify the schema.org type for itemListElement to be a set which bypasses the issue
|
|
1650
|
+
// https://github.com/digitalbazaar/jsonld.js/issues/247
|
|
1651
|
+
overrideContext = {
|
|
1652
|
+
itemListElement: {
|
|
1653
|
+
"@id": "http://schema.org/itemListElement",
|
|
1654
|
+
"@container": "@set",
|
|
1655
|
+
"@protected": true
|
|
1608
1656
|
}
|
|
1657
|
+
};
|
|
1658
|
+
if (Is.object(context) && "@context" in context) {
|
|
1659
|
+
// If the context is an object, we need to merge it with the override context
|
|
1660
|
+
context = JsonLdProcessor.combineContexts(context["@context"], overrideContext);
|
|
1609
1661
|
}
|
|
1610
|
-
else
|
|
1611
|
-
context
|
|
1662
|
+
else {
|
|
1663
|
+
// If the context is a string or an array, we need to merge it with the override context
|
|
1664
|
+
context = JsonLdProcessor.combineContexts(context, overrideContext);
|
|
1612
1665
|
}
|
|
1613
1666
|
}
|
|
1614
1667
|
const compacted = await jsonLd.compact(ObjectHelper.removeEmptyProperties(document), context, {
|
|
1615
1668
|
documentLoader: JsonLdProcessor.getDocumentLoader()
|
|
1616
1669
|
});
|
|
1670
|
+
if (!Is.empty(overrideContext)) {
|
|
1671
|
+
// Remove the override context from the compacted document
|
|
1672
|
+
compacted["@context"] = JsonLdProcessor.removeContexts(compacted["@context"], [overrideContext]);
|
|
1673
|
+
}
|
|
1617
1674
|
return compacted;
|
|
1618
1675
|
}
|
|
1619
1676
|
return document;
|
|
@@ -1827,17 +1884,34 @@ class JsonLdProcessor {
|
|
|
1827
1884
|
break;
|
|
1828
1885
|
}
|
|
1829
1886
|
}
|
|
1830
|
-
|
|
1831
|
-
|
|
1832
|
-
|
|
1833
|
-
|
|
1834
|
-
|
|
1887
|
+
try {
|
|
1888
|
+
const response = await FetchHelper.fetchJson(JsonLdProcessor._CLASS_NAME, url, HttpMethod.GET, undefined, {
|
|
1889
|
+
cacheTtlMs: JsonLdProcessor.getCacheLimit(),
|
|
1890
|
+
headers: {
|
|
1891
|
+
[HeaderTypes.Accept]: MimeTypes.JsonLd
|
|
1892
|
+
}
|
|
1893
|
+
});
|
|
1894
|
+
return {
|
|
1895
|
+
documentUrl: url,
|
|
1896
|
+
document: response
|
|
1897
|
+
};
|
|
1898
|
+
}
|
|
1899
|
+
catch (err) {
|
|
1900
|
+
const error = BaseError.fromError(err);
|
|
1901
|
+
if (error.message.includes("is not valid JSON")) {
|
|
1902
|
+
const response = await FetchHelper.fetchJson(JsonLdProcessor._CLASS_NAME, url, HttpMethod.GET, undefined, {
|
|
1903
|
+
cacheTtlMs: JsonLdProcessor.getCacheLimit(),
|
|
1904
|
+
headers: {
|
|
1905
|
+
[HeaderTypes.Accept]: MimeTypes.Json
|
|
1906
|
+
}
|
|
1907
|
+
});
|
|
1908
|
+
return {
|
|
1909
|
+
documentUrl: url,
|
|
1910
|
+
document: response
|
|
1911
|
+
};
|
|
1835
1912
|
}
|
|
1836
|
-
|
|
1837
|
-
|
|
1838
|
-
documentUrl: url,
|
|
1839
|
-
document: response
|
|
1840
|
-
};
|
|
1913
|
+
throw err;
|
|
1914
|
+
}
|
|
1841
1915
|
}
|
|
1842
1916
|
/**
|
|
1843
1917
|
* Handle common errors.
|
|
@@ -1856,4 +1930,43 @@ class JsonLdProcessor {
|
|
|
1856
1930
|
}
|
|
1857
1931
|
}
|
|
1858
1932
|
|
|
1933
|
+
// Copyright 2024 IOTA Stiftung.
|
|
1934
|
+
// SPDX-License-Identifier: Apache-2.0.
|
|
1935
|
+
/**
|
|
1936
|
+
* Class to help with JSON LD.
|
|
1937
|
+
*/
|
|
1938
|
+
class JsonLdHelper {
|
|
1939
|
+
/**
|
|
1940
|
+
* Validate a JSON-LD document.
|
|
1941
|
+
* @param document The JSON-LD document to validate.
|
|
1942
|
+
* @param validationFailures The list of validation failures to add to.
|
|
1943
|
+
* @param validationMode The validation mode to use, defaults to either.
|
|
1944
|
+
* @returns True if the document was valid.
|
|
1945
|
+
*/
|
|
1946
|
+
static async validate(document, validationFailures, validationMode) {
|
|
1947
|
+
if (Is.array(document)) {
|
|
1948
|
+
// If the document is an array of nodes, validate each node
|
|
1949
|
+
for (const node of document) {
|
|
1950
|
+
await JsonLdHelper.validate(node, validationFailures, validationMode);
|
|
1951
|
+
}
|
|
1952
|
+
}
|
|
1953
|
+
else if (Is.array(document["@graph"])) {
|
|
1954
|
+
// If the graph is an array of nodes, validate each node
|
|
1955
|
+
for (const node of document["@graph"]) {
|
|
1956
|
+
await JsonLdHelper.validate(node, validationFailures, validationMode);
|
|
1957
|
+
}
|
|
1958
|
+
}
|
|
1959
|
+
else if (Is.object(document)) {
|
|
1960
|
+
// Expand the document to ensure we have the full context for types
|
|
1961
|
+
// As the data types in the factories are not always fully qualified
|
|
1962
|
+
const expandedDoc = await JsonLdProcessor.expand(document);
|
|
1963
|
+
const expandedDataType = ArrayHelper.fromObjectOrArray(expandedDoc[0]["@type"]);
|
|
1964
|
+
if (Is.arrayValue(expandedDataType)) {
|
|
1965
|
+
await DataTypeHelper.validate("document", expandedDataType[0], document, validationFailures, validationMode);
|
|
1966
|
+
}
|
|
1967
|
+
}
|
|
1968
|
+
return validationFailures.length === 0;
|
|
1969
|
+
}
|
|
1970
|
+
}
|
|
1971
|
+
|
|
1859
1972
|
export { JsonLdContexts, JsonLdDataTypes, JsonLdHelper, JsonLdProcessor, JsonLdTypes };
|
|
@@ -62,6 +62,14 @@ export declare const JsonLdTypes: {
|
|
|
62
62
|
* Represents JSON-LD Context Definition.
|
|
63
63
|
*/
|
|
64
64
|
readonly ContextDefinition: "JsonLdContextDefinition";
|
|
65
|
+
/**
|
|
66
|
+
* Represents JSON-LD Context Definition Element.
|
|
67
|
+
*/
|
|
68
|
+
readonly ContextDefinitionElement: "JsonLdContextDefinitionElement";
|
|
69
|
+
/**
|
|
70
|
+
* Represents JSON-LD Context Definition Root.
|
|
71
|
+
*/
|
|
72
|
+
readonly ContextDefinitionRoot: "JsonLdContextDefinitionRoot";
|
|
65
73
|
/**
|
|
66
74
|
* Represents JSON-LD Expanded Term Definition.
|
|
67
75
|
*/
|
|
@@ -45,10 +45,14 @@ export declare class JsonLdProcessor {
|
|
|
45
45
|
/**
|
|
46
46
|
* Compact a document according to a particular context.
|
|
47
47
|
* @param document The JSON-LD document to compact.
|
|
48
|
-
* @param context The context to compact the document to, if not provided will
|
|
48
|
+
* @param context The context to compact the document to, if not provided will use the one in the document.
|
|
49
|
+
* @param options The options for compacting the document.
|
|
50
|
+
* @param options.itemListOverride Whether to override the itemListElement context with a set, defaults to true.
|
|
49
51
|
* @returns The compacted JSON-LD document.
|
|
50
52
|
*/
|
|
51
|
-
static compact<T>(document: T, context?: IJsonLdContextDefinitionRoot
|
|
53
|
+
static compact<T>(document: T, context?: IJsonLdContextDefinitionRoot, options?: {
|
|
54
|
+
itemListOverride: boolean;
|
|
55
|
+
}): Promise<T>;
|
|
52
56
|
/**
|
|
53
57
|
* Expand a document, removing its context.
|
|
54
58
|
* @param compacted The compacted JSON-LD document to expand.
|
package/docs/changelog.md
CHANGED
|
@@ -1,5 +1,33 @@
|
|
|
1
1
|
# @twin.org/data-json-ld - Changelog
|
|
2
2
|
|
|
3
|
+
## [0.0.1-next.32](https://github.com/twinfoundation/data/compare/data-json-ld-v0.0.1-next.31...data-json-ld-v0.0.1-next.32) (2025-05-28)
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
### Features
|
|
7
|
+
|
|
8
|
+
* use fully qualified names for data type lookups ([b7b5c74](https://github.com/twinfoundation/data/commit/b7b5c746b0180a87baa976f6a7a76cedd53d8ff7))
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
### Dependencies
|
|
12
|
+
|
|
13
|
+
* The following workspace dependencies were updated
|
|
14
|
+
* dependencies
|
|
15
|
+
* @twin.org/data-core bumped from 0.0.1-next.31 to 0.0.1-next.32
|
|
16
|
+
|
|
17
|
+
## [0.0.1-next.31](https://github.com/twinfoundation/data/compare/data-json-ld-v0.0.1-next.30...data-json-ld-v0.0.1-next.31) (2025-05-08)
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
### Bug Fixes
|
|
21
|
+
|
|
22
|
+
* itemListElement compaction override ([d908a10](https://github.com/twinfoundation/data/commit/d908a1043d7792e31b3101221d17850757b6c2a6))
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
### Dependencies
|
|
26
|
+
|
|
27
|
+
* The following workspace dependencies were updated
|
|
28
|
+
* dependencies
|
|
29
|
+
* @twin.org/data-core bumped from 0.0.1-next.30 to 0.0.1-next.31
|
|
30
|
+
|
|
3
31
|
## [0.0.1-next.30](https://github.com/twinfoundation/data/compare/data-json-ld-v0.0.1-next.29...data-json-ld-v0.0.1-next.30) (2025-04-17)
|
|
4
32
|
|
|
5
33
|
|
|
@@ -128,7 +128,7 @@ The registered redirects.
|
|
|
128
128
|
|
|
129
129
|
### compact()
|
|
130
130
|
|
|
131
|
-
> `static` **compact**\<`T`\>(`document`, `context?`): `Promise`\<`T`\>
|
|
131
|
+
> `static` **compact**\<`T`\>(`document`, `context?`, `options?`): `Promise`\<`T`\>
|
|
132
132
|
|
|
133
133
|
Compact a document according to a particular context.
|
|
134
134
|
|
|
@@ -150,7 +150,17 @@ The JSON-LD document to compact.
|
|
|
150
150
|
|
|
151
151
|
[`IJsonLdContextDefinitionRoot`](../type-aliases/IJsonLdContextDefinitionRoot.md)
|
|
152
152
|
|
|
153
|
-
The context to compact the document to, if not provided will
|
|
153
|
+
The context to compact the document to, if not provided will use the one in the document.
|
|
154
|
+
|
|
155
|
+
##### options?
|
|
156
|
+
|
|
157
|
+
The options for compacting the document.
|
|
158
|
+
|
|
159
|
+
###### itemListOverride
|
|
160
|
+
|
|
161
|
+
`boolean`
|
|
162
|
+
|
|
163
|
+
Whether to override the itemListElement context with a set, defaults to true.
|
|
154
164
|
|
|
155
165
|
#### Returns
|
|
156
166
|
|
|
@@ -96,6 +96,18 @@ Represents JSON-LD Included block.
|
|
|
96
96
|
|
|
97
97
|
Represents JSON-LD Context Definition.
|
|
98
98
|
|
|
99
|
+
### ContextDefinitionElement
|
|
100
|
+
|
|
101
|
+
> `readonly` **ContextDefinitionElement**: `"JsonLdContextDefinitionElement"` = `"JsonLdContextDefinitionElement"`
|
|
102
|
+
|
|
103
|
+
Represents JSON-LD Context Definition Element.
|
|
104
|
+
|
|
105
|
+
### ContextDefinitionRoot
|
|
106
|
+
|
|
107
|
+
> `readonly` **ContextDefinitionRoot**: `"JsonLdContextDefinitionRoot"` = `"JsonLdContextDefinitionRoot"`
|
|
108
|
+
|
|
109
|
+
Represents JSON-LD Context Definition Root.
|
|
110
|
+
|
|
99
111
|
### ExpandedTermDefinition
|
|
100
112
|
|
|
101
113
|
> `readonly` **ExpandedTermDefinition**: `"JsonLdExpandedTermDefinition"` = `"JsonLdExpandedTermDefinition"`
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@twin.org/data-json-ld",
|
|
3
|
-
"version": "0.0.1-next.
|
|
3
|
+
"version": "0.0.1-next.32",
|
|
4
4
|
"description": "Models which define the structure of JSON LD",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
},
|
|
16
16
|
"dependencies": {
|
|
17
17
|
"@twin.org/core": "next",
|
|
18
|
-
"@twin.org/data-core": "0.0.1-next.
|
|
18
|
+
"@twin.org/data-core": "0.0.1-next.32",
|
|
19
19
|
"@twin.org/entity": "next",
|
|
20
20
|
"@twin.org/nameof": "next",
|
|
21
21
|
"@twin.org/web": "next",
|