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