@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.
@@ -649,7 +649,70 @@ var JsonSchema = (function (exports) {
649
649
  return resolvedUrl;
650
650
  };
651
651
 
652
- 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 };
653
716
 
654
717
  const nil$2 = "";
655
718
 
@@ -1155,7 +1218,7 @@ var JsonSchema = (function (exports) {
1155
1218
 
1156
1219
  var fetch_browser = fetch;
1157
1220
 
1158
- const { jsonTypeOf, splitUrl: splitUrl$3, safeResolveUrl } = common$1;
1221
+ const { jsonTypeOf, splitUrl: splitUrl$3, safeResolveUrl, pathRelative } = common$1;
1159
1222
 
1160
1223
 
1161
1224
 
@@ -1385,10 +1448,73 @@ var JsonSchema = (function (exports) {
1385
1448
 
1386
1449
  const length = (doc) => value(doc).length;
1387
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
+
1388
1513
  var schema$5 = {
1389
1514
  setConfig, getConfig,
1390
1515
  add: add$1, get, markValidated,
1391
- uri, value, getAnchorPointer, typeOf, has, step, keys, entries, map, length
1516
+ uri, value, getAnchorPointer, typeOf, has, step, keys, entries, map, length,
1517
+ toSchema
1392
1518
  };
1393
1519
  schema$5.setConfig;
1394
1520
  schema$5.getConfig;
@@ -1405,6 +1531,7 @@ var JsonSchema = (function (exports) {
1405
1531
  schema$5.entries;
1406
1532
  schema$5.map;
1407
1533
  schema$5.length;
1534
+ schema$5.toSchema;
1408
1535
 
1409
1536
  class InvalidSchemaError$1 extends Error {
1410
1537
  constructor(output) {