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