@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.
@@ -646,7 +646,70 @@ const safeResolveUrl$1 = (contextUrl, url) => {
646
646
  return resolvedUrl;
647
647
  };
648
648
 
649
- 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 };
650
713
 
651
714
  const nil$2 = "";
652
715
 
@@ -1152,7 +1215,7 @@ lib$2.allValues;
1152
1215
 
1153
1216
  var fetch_browser = fetch;
1154
1217
 
1155
- const { jsonTypeOf, splitUrl: splitUrl$3, safeResolveUrl } = common$1;
1218
+ const { jsonTypeOf, splitUrl: splitUrl$3, safeResolveUrl, pathRelative } = common$1;
1156
1219
 
1157
1220
 
1158
1221
 
@@ -1382,10 +1445,73 @@ const map = justCurryIt((fn, doc) => lib$2.pipeline([
1382
1445
 
1383
1446
  const length = (doc) => value(doc).length;
1384
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
+
1385
1510
  var schema$5 = {
1386
1511
  setConfig, getConfig,
1387
1512
  add: add$1, get, markValidated,
1388
- uri, value, getAnchorPointer, typeOf, has, step, keys, entries, map, length
1513
+ uri, value, getAnchorPointer, typeOf, has, step, keys, entries, map, length,
1514
+ toSchema
1389
1515
  };
1390
1516
  schema$5.setConfig;
1391
1517
  schema$5.getConfig;
@@ -1402,6 +1528,7 @@ schema$5.keys;
1402
1528
  schema$5.entries;
1403
1529
  schema$5.map;
1404
1530
  schema$5.length;
1531
+ schema$5.toSchema;
1405
1532
 
1406
1533
  class InvalidSchemaError$1 extends Error {
1407
1534
  constructor(output) {