@hyperjump/json-schema 0.18.2 → 0.18.5

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.
@@ -58,8 +58,14 @@ define(['exports'], (function (exports) { 'use strict';
58
58
  (function (root, factory){
59
59
 
60
60
  var PubSub = {};
61
- root.PubSub = PubSub;
62
- factory(PubSub);
61
+
62
+ if (root.PubSub) {
63
+ PubSub = root.PubSub;
64
+ console.warn("PubSub already loaded, using existing version");
65
+ } else {
66
+ root.PubSub = PubSub;
67
+ factory(PubSub);
68
+ }
63
69
  // CommonJS and Node.js module support
64
70
  {
65
71
  if (module !== undefined && module.exports) {
@@ -642,7 +648,70 @@ define(['exports'], (function (exports) { 'use strict';
642
648
  return resolvedUrl;
643
649
  };
644
650
 
645
- var common$1 = { jsonTypeOf: jsonTypeOf$2, splitUrl: splitUrl$4, safeResolveUrl: safeResolveUrl$1 };
651
+ const CHAR_BACKWARD_SLASH = 47;
652
+
653
+ const pathRelative$1 = (from, to) => {
654
+ if (from === to) {
655
+ return "";
656
+ }
657
+
658
+ let toStart = 1;
659
+ const fromLen = from.length - 1;
660
+ const toLen = to.length - toStart;
661
+
662
+ // Compare paths to find the longest common path from root
663
+ const length = fromLen < toLen ? fromLen : toLen;
664
+ let lastCommonSep = -1;
665
+ let i = 0;
666
+ for (; i < length; i++) {
667
+ const fromCode = from.charCodeAt(i + 1);
668
+ if (fromCode !== to.charCodeAt(toStart + i)) {
669
+ break;
670
+ } else if (fromCode === CHAR_BACKWARD_SLASH) {
671
+ lastCommonSep = i;
672
+ }
673
+ }
674
+
675
+ if (toLen > length) {
676
+ if (to.charCodeAt(toStart + i) === CHAR_BACKWARD_SLASH) {
677
+ return to.slice(toStart + i + 1);
678
+ }
679
+ if (i === 0) {
680
+ return to.slice(toStart + i);
681
+ }
682
+ }
683
+ if (fromLen > length) {
684
+ if (from.charCodeAt(i + 1) === CHAR_BACKWARD_SLASH) {
685
+ lastCommonSep = i;
686
+ } else if (length === 0) {
687
+ lastCommonSep = 0;
688
+ }
689
+ }
690
+
691
+ let out = "";
692
+ // Generate the relative path based on the path difference between `to` and `from`
693
+ for (i = lastCommonSep + 2; i <= from.length; ++i) {
694
+ if (i === from.length || from.charCodeAt(i) === CHAR_BACKWARD_SLASH) {
695
+ out += out.length === 0 ? ".." : "/..";
696
+ }
697
+ }
698
+
699
+ toStart += lastCommonSep;
700
+
701
+ // Lastly, append the rest of the destination (`to`) path that comes after
702
+ // the common path parts
703
+ if (out.length > 0) {
704
+ return `${out}${to.slice(toStart, to.length)}`;
705
+ }
706
+
707
+ if (to.charCodeAt(toStart) === CHAR_BACKWARD_SLASH) {
708
+ ++toStart;
709
+ }
710
+
711
+ return to.slice(toStart, to.length);
712
+ };
713
+
714
+ var common$1 = { jsonTypeOf: jsonTypeOf$2, splitUrl: splitUrl$4, safeResolveUrl: safeResolveUrl$1, pathRelative: pathRelative$1 };
646
715
 
647
716
  const nil$2 = "";
648
717
 
@@ -795,6 +864,10 @@ define(['exports'], (function (exports) { 'use strict';
795
864
  const value$2 = (ref) => ref[$__value];
796
865
 
797
866
  var reference = { cons: cons$1, isReference, href, value: value$2 };
867
+ reference.cons;
868
+ reference.isReference;
869
+ reference.href;
870
+ reference.value;
798
871
 
799
872
  const { jsonTypeOf: jsonTypeOf$1 } = common$1;
800
873
 
@@ -1144,7 +1217,7 @@ define(['exports'], (function (exports) { 'use strict';
1144
1217
 
1145
1218
  var fetch_browser = fetch;
1146
1219
 
1147
- const { jsonTypeOf, splitUrl: splitUrl$3, safeResolveUrl } = common$1;
1220
+ const { jsonTypeOf, splitUrl: splitUrl$3, safeResolveUrl, pathRelative } = common$1;
1148
1221
 
1149
1222
 
1150
1223
 
@@ -1374,10 +1447,73 @@ define(['exports'], (function (exports) { 'use strict';
1374
1447
 
1375
1448
  const length = (doc) => value(doc).length;
1376
1449
 
1450
+ const toSchemaDefaultOptions = {
1451
+ parentId: "",
1452
+ parentDialect: "",
1453
+ includeEmbedded: true
1454
+ };
1455
+ const toSchema = (schemaDoc, options = {}) => {
1456
+ const fullOptions = { ...toSchemaDefaultOptions, ...options };
1457
+
1458
+ const schema = JSON.parse(JSON.stringify(schemaDoc.schema, (key, value) => {
1459
+ if (!reference.isReference(value)) {
1460
+ return value;
1461
+ }
1462
+
1463
+ const refValue = reference.value(value);
1464
+ const embeddedDialect = refValue.$schema || schemaDoc.schemaVersion;
1465
+ const embeddedToken = getConfig(embeddedDialect, "embeddedToken");
1466
+ if (!fullOptions.includeEmbedded && embeddedToken in refValue) {
1467
+ return;
1468
+ } else {
1469
+ return reference.value(value);
1470
+ }
1471
+ }));
1472
+
1473
+ const dynamicAnchorToken = getConfig(schemaDoc.schemaVersion, "dynamicAnchorToken");
1474
+ Object.entries(schemaDoc.dynamicAnchors)
1475
+ .forEach(([anchor, uri]) => {
1476
+ const pointer = splitUrl$3(uri)[1];
1477
+ lib$3.assign(pointer, schema, {
1478
+ [dynamicAnchorToken]: anchor,
1479
+ ...lib$3.get(pointer, schema)
1480
+ });
1481
+ });
1482
+
1483
+ const anchorToken = getConfig(schemaDoc.schemaVersion, "anchorToken");
1484
+ Object.entries(schemaDoc.anchors)
1485
+ .filter(([anchor]) => anchor !== "")
1486
+ .forEach(([anchor, pointer]) => {
1487
+ lib$3.assign(pointer, schema, {
1488
+ [anchorToken]: anchor,
1489
+ ...lib$3.get(pointer, schema)
1490
+ });
1491
+ });
1492
+
1493
+ const baseToken = getConfig(schemaDoc.schemaVersion, "baseToken");
1494
+ const id = relativeUri(fullOptions.parentId, schemaDoc.id);
1495
+ const dialect = fullOptions.parentDialect === schemaDoc.schemaVersion ? "" : schemaDoc.schemaVersion;
1496
+ return {
1497
+ ...(id && { [baseToken]: id }),
1498
+ ...(dialect && { $schema: dialect }),
1499
+ ...schema
1500
+ };
1501
+ };
1502
+
1503
+ const relativeUri = (from, to) => {
1504
+ if (to.startsWith("file://")) {
1505
+ const pathToSchema = from.slice(7, from.lastIndexOf("/"));
1506
+ return from === "" ? "" : pathRelative(pathToSchema, to.slice(7));
1507
+ } else {
1508
+ return to;
1509
+ }
1510
+ };
1511
+
1377
1512
  var schema$5 = {
1378
1513
  setConfig, getConfig,
1379
1514
  add: add$1, get, markValidated,
1380
- uri, value, getAnchorPointer, typeOf, has, step, keys, entries, map, length
1515
+ uri, value, getAnchorPointer, typeOf, has, step, keys, entries, map, length,
1516
+ toSchema
1381
1517
  };
1382
1518
  schema$5.setConfig;
1383
1519
  schema$5.getConfig;
@@ -1394,6 +1530,7 @@ define(['exports'], (function (exports) { 'use strict';
1394
1530
  schema$5.entries;
1395
1531
  schema$5.map;
1396
1532
  schema$5.length;
1533
+ schema$5.toSchema;
1397
1534
 
1398
1535
  class InvalidSchemaError$1 extends Error {
1399
1536
  constructor(output) {
@@ -1722,7 +1859,7 @@ define(['exports'], (function (exports) { 'use strict';
1722
1859
  keywords$1.metaData;
1723
1860
  keywords$1.validate;
1724
1861
 
1725
- var lib$1 = { Core: core$2, Schema: schema$5, Instance: instance, Keywords: keywords$1, InvalidSchemaError: invalidSchemaError };
1862
+ var lib$1 = { Core: core$2, Schema: schema$5, Instance: instance, Reference: reference, Keywords: keywords$1, InvalidSchemaError: invalidSchemaError };
1726
1863
 
1727
1864
  const { Core: Core$v, Schema: Schema$N, Instance: Instance$B } = lib$1;
1728
1865