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