@hyperjump/json-schema 0.18.4 → 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.
@@ -652,7 +652,70 @@
652
652
  return resolvedUrl;
653
653
  };
654
654
 
655
- var common$1 = { jsonTypeOf: jsonTypeOf$2, splitUrl: splitUrl$4, safeResolveUrl: safeResolveUrl$1 };
655
+ const CHAR_BACKWARD_SLASH = 47;
656
+
657
+ const pathRelative$1 = (from, to) => {
658
+ if (from === to) {
659
+ return "";
660
+ }
661
+
662
+ let toStart = 1;
663
+ const fromLen = from.length - 1;
664
+ const toLen = to.length - toStart;
665
+
666
+ // Compare paths to find the longest common path from root
667
+ const length = fromLen < toLen ? fromLen : toLen;
668
+ let lastCommonSep = -1;
669
+ let i = 0;
670
+ for (; i < length; i++) {
671
+ const fromCode = from.charCodeAt(i + 1);
672
+ if (fromCode !== to.charCodeAt(toStart + i)) {
673
+ break;
674
+ } else if (fromCode === CHAR_BACKWARD_SLASH) {
675
+ lastCommonSep = i;
676
+ }
677
+ }
678
+
679
+ if (toLen > length) {
680
+ if (to.charCodeAt(toStart + i) === CHAR_BACKWARD_SLASH) {
681
+ return to.slice(toStart + i + 1);
682
+ }
683
+ if (i === 0) {
684
+ return to.slice(toStart + i);
685
+ }
686
+ }
687
+ if (fromLen > length) {
688
+ if (from.charCodeAt(i + 1) === CHAR_BACKWARD_SLASH) {
689
+ lastCommonSep = i;
690
+ } else if (length === 0) {
691
+ lastCommonSep = 0;
692
+ }
693
+ }
694
+
695
+ let out = "";
696
+ // Generate the relative path based on the path difference between `to` and `from`
697
+ for (i = lastCommonSep + 2; i <= from.length; ++i) {
698
+ if (i === from.length || from.charCodeAt(i) === CHAR_BACKWARD_SLASH) {
699
+ out += out.length === 0 ? ".." : "/..";
700
+ }
701
+ }
702
+
703
+ toStart += lastCommonSep;
704
+
705
+ // Lastly, append the rest of the destination (`to`) path that comes after
706
+ // the common path parts
707
+ if (out.length > 0) {
708
+ return `${out}${to.slice(toStart, to.length)}`;
709
+ }
710
+
711
+ if (to.charCodeAt(toStart) === CHAR_BACKWARD_SLASH) {
712
+ ++toStart;
713
+ }
714
+
715
+ return to.slice(toStart, to.length);
716
+ };
717
+
718
+ var common$1 = { jsonTypeOf: jsonTypeOf$2, splitUrl: splitUrl$4, safeResolveUrl: safeResolveUrl$1, pathRelative: pathRelative$1 };
656
719
 
657
720
  const nil$2 = "";
658
721
 
@@ -1158,7 +1221,7 @@
1158
1221
 
1159
1222
  var fetch_browser = fetch;
1160
1223
 
1161
- const { jsonTypeOf, splitUrl: splitUrl$3, safeResolveUrl } = common$1;
1224
+ const { jsonTypeOf, splitUrl: splitUrl$3, safeResolveUrl, pathRelative } = common$1;
1162
1225
 
1163
1226
 
1164
1227
 
@@ -1388,10 +1451,73 @@
1388
1451
 
1389
1452
  const length = (doc) => value(doc).length;
1390
1453
 
1454
+ const toSchemaDefaultOptions = {
1455
+ parentId: "",
1456
+ parentDialect: "",
1457
+ includeEmbedded: true
1458
+ };
1459
+ const toSchema = (schemaDoc, options = {}) => {
1460
+ const fullOptions = { ...toSchemaDefaultOptions, ...options };
1461
+
1462
+ const schema = JSON.parse(JSON.stringify(schemaDoc.schema, (key, value) => {
1463
+ if (!reference.isReference(value)) {
1464
+ return value;
1465
+ }
1466
+
1467
+ const refValue = reference.value(value);
1468
+ const embeddedDialect = refValue.$schema || schemaDoc.schemaVersion;
1469
+ const embeddedToken = getConfig(embeddedDialect, "embeddedToken");
1470
+ if (!fullOptions.includeEmbedded && embeddedToken in refValue) {
1471
+ return;
1472
+ } else {
1473
+ return reference.value(value);
1474
+ }
1475
+ }));
1476
+
1477
+ const dynamicAnchorToken = getConfig(schemaDoc.schemaVersion, "dynamicAnchorToken");
1478
+ Object.entries(schemaDoc.dynamicAnchors)
1479
+ .forEach(([anchor, uri]) => {
1480
+ const pointer = splitUrl$3(uri)[1];
1481
+ lib$3.assign(pointer, schema, {
1482
+ [dynamicAnchorToken]: anchor,
1483
+ ...lib$3.get(pointer, schema)
1484
+ });
1485
+ });
1486
+
1487
+ const anchorToken = getConfig(schemaDoc.schemaVersion, "anchorToken");
1488
+ Object.entries(schemaDoc.anchors)
1489
+ .filter(([anchor]) => anchor !== "")
1490
+ .forEach(([anchor, pointer]) => {
1491
+ lib$3.assign(pointer, schema, {
1492
+ [anchorToken]: anchor,
1493
+ ...lib$3.get(pointer, schema)
1494
+ });
1495
+ });
1496
+
1497
+ const baseToken = getConfig(schemaDoc.schemaVersion, "baseToken");
1498
+ const id = relativeUri(fullOptions.parentId, schemaDoc.id);
1499
+ const dialect = fullOptions.parentDialect === schemaDoc.schemaVersion ? "" : schemaDoc.schemaVersion;
1500
+ return {
1501
+ ...(id && { [baseToken]: id }),
1502
+ ...(dialect && { $schema: dialect }),
1503
+ ...schema
1504
+ };
1505
+ };
1506
+
1507
+ const relativeUri = (from, to) => {
1508
+ if (to.startsWith("file://")) {
1509
+ const pathToSchema = from.slice(7, from.lastIndexOf("/"));
1510
+ return from === "" ? "" : pathRelative(pathToSchema, to.slice(7));
1511
+ } else {
1512
+ return to;
1513
+ }
1514
+ };
1515
+
1391
1516
  var schema$5 = {
1392
1517
  setConfig, getConfig,
1393
1518
  add: add$1, get, markValidated,
1394
- uri, value, getAnchorPointer, typeOf, has, step, keys, entries, map, length
1519
+ uri, value, getAnchorPointer, typeOf, has, step, keys, entries, map, length,
1520
+ toSchema
1395
1521
  };
1396
1522
  schema$5.setConfig;
1397
1523
  schema$5.getConfig;
@@ -1408,6 +1534,7 @@
1408
1534
  schema$5.entries;
1409
1535
  schema$5.map;
1410
1536
  schema$5.length;
1537
+ schema$5.toSchema;
1411
1538
 
1412
1539
  class InvalidSchemaError$1 extends Error {
1413
1540
  constructor(output) {