@amritk/helpers 0.15.0 → 0.15.2

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.
@@ -1,3 +1,4 @@
1
+ import { DATA_KEYWORDS } from "./build-resource-registry.js";
1
2
  import { assertSchemaDepth } from "./max-schema-depth.js";
2
3
  import { refToFilename, toKebabCase } from "./ref-to-filename.js";
3
4
  const isDraft07Schema = (schema) => typeof schema["$schema"] === "string" && schema["$schema"].includes("draft-07");
@@ -72,10 +73,29 @@ const upgradeDraft07Schema = (schema) => {
72
73
  }
73
74
  }
74
75
  return {
75
- ...rest,
76
+ ...rewriteDefinitionsRefs(rest),
76
77
  $defs: hoistedDefs
77
78
  };
78
79
  };
80
+ const rewriteDefinitionsRefs = (obj, depth = 0) => {
81
+ assertSchemaDepth(depth, "upgradeDraft07Schema");
82
+ if (typeof obj !== "object" || obj === null)
83
+ return obj;
84
+ if (Array.isArray(obj))
85
+ return obj.map((item) => rewriteDefinitionsRefs(item, depth + 1));
86
+ const record = obj;
87
+ const result = {};
88
+ for (const [key, value] of Object.entries(record)) {
89
+ const rewritten = key === "$ref" && typeof value === "string" && value.startsWith("#/definitions/") ? value.replace("#/definitions/", "#/$defs/") : DATA_KEYWORDS.has(key) || startsNewResource(value) ? value : rewriteDefinitionsRefs(value, depth + 1);
90
+ if (key === "__proto__") {
91
+ Object.defineProperty(result, key, { value: rewritten, writable: true, enumerable: true, configurable: true });
92
+ } else {
93
+ result[key] = rewritten;
94
+ }
95
+ }
96
+ return result;
97
+ };
98
+ const startsNewResource = (value) => typeof value === "object" && value !== null && !Array.isArray(value) && typeof value["$id"] === "string";
79
99
  const renameNestedDefs = (obj, depth = 0) => {
80
100
  assertSchemaDepth(depth, "upgradeDraft07Schema");
81
101
  if (typeof obj !== "object" || obj === null)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@amritk/helpers",
3
- "version": "0.15.0",
3
+ "version": "0.15.2",
4
4
  "description": "Shared utilities for the mjst code generation ecosystem.",
5
5
  "type": "module",
6
6
  "sideEffects": false,
@@ -36,6 +36,7 @@
36
36
  },
37
37
  "scripts": {
38
38
  "build": "tsgo -p tsconfig.build.json && tsc-alias -p tsconfig.build.json -f && node ../../scripts/strip-comments.mjs",
39
+ "prepublishOnly": "node ../../scripts/check-publishable.mjs",
39
40
  "types:check": "tsgo -p . --noEmit",
40
41
  "test": "NODE_ENV=production vitest run --root ../.. helpers"
41
42
  },