@json-schema-tools/referencer 1.0.6 → 1.1.1

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.
package/README.md CHANGED
@@ -17,6 +17,7 @@ The input schema, as well as all of its subschemas must have titles. Their title
17
17
  Features:
18
18
  - Cyclic schemas become cyclic references
19
19
  - Completely synchronous
20
+ - immutable by default, option to mutate in place.
20
21
  - No external dependencies
21
22
  - Fully typed against the generated [meta-schema typings](https://github.com/json-schema-tools/meta-schema/)
22
23
  - magically makes your json schema smaller.
package/build/index.d.ts CHANGED
@@ -5,6 +5,15 @@ export declare class NoTitleError implements Error {
5
5
  message: string;
6
6
  constructor(schema: JSONSchema, parentSchema: JSONSchema);
7
7
  }
8
+ /**
9
+ * Options that can be passed into the Referencer.
10
+ */
11
+ export interface ReferencerOptions {
12
+ /**
13
+ * If true, the schema passed in will be referenced in-place, mutating the original. Default is false.
14
+ */
15
+ mutate?: boolean;
16
+ }
8
17
  /**
9
18
  * Returns the schema where all subschemas have been replaced with $refs and added to definitions.
10
19
  *
@@ -20,4 +29,4 @@ export declare class NoTitleError implements Error {
20
29
  * @category SchemaImprover
21
30
  *
22
31
  */
23
- export default function referencer(s: JSONSchema): JSONSchema;
32
+ export default function referencer(s: JSONSchema, options?: ReferencerOptions): JSONSchema;
package/build/index.js CHANGED
@@ -18,7 +18,8 @@ exports.NoTitleError = exports.stringifyCircular = void 0;
18
18
  var traverse_1 = __importDefault(require("@json-schema-tools/traverse"));
19
19
  var deleteAllProps = function (o) {
20
20
  Object.keys(o)
21
- .forEach(function (k) { delete o[k]; });
21
+ .forEach(function (k) { if (k !== 'title')
22
+ delete o[k]; });
22
23
  };
23
24
  var stringifyCircular = function (obj) {
24
25
  var cache = [];
@@ -64,6 +65,9 @@ var NoTitleError = /** @class */ (function () {
64
65
  return NoTitleError;
65
66
  }());
66
67
  exports.NoTitleError = NoTitleError;
68
+ var defaultReferencerOptions = {
69
+ mutate: false,
70
+ };
67
71
  /**
68
72
  * Returns the schema where all subschemas have been replaced with $refs and added to definitions.
69
73
  *
@@ -79,9 +83,10 @@ exports.NoTitleError = NoTitleError;
79
83
  * @category SchemaImprover
80
84
  *
81
85
  */
82
- function referencer(s) {
86
+ function referencer(s, options) {
87
+ if (options === void 0) { options = defaultReferencerOptions; }
83
88
  var definitions = {};
84
- (0, traverse_1.default)(s, function (subSchema, isRootCycle) {
89
+ var newS = (0, traverse_1.default)(s, function (subSchema, isRootCycle) {
85
90
  var t = "";
86
91
  if (isRootCycle && subSchema !== true && subSchema !== false) {
87
92
  if (subSchema.$ref) {
@@ -97,7 +102,6 @@ function referencer(s) {
97
102
  return { $ref: "#/definitions/".concat(s.title) };
98
103
  }
99
104
  definitions[subSchema.title] = __assign({}, subSchema);
100
- deleteAllProps(subSchema);
101
105
  subSchema.$ref = "#/definitions/".concat(subSchema.title);
102
106
  return subSchema;
103
107
  }
@@ -122,10 +126,13 @@ function referencer(s) {
122
126
  subSchema.$ref = "#/definitions/".concat(t);
123
127
  }
124
128
  return subSchema;
125
- }, { mutable: true, skipFirstMutation: true });
126
- if (typeof s === "object" && Object.keys(definitions).length > 0) {
127
- s.definitions = __assign(__assign({}, s.definitions), definitions);
129
+ }, { mutable: options.mutate, skipFirstMutation: true });
130
+ if (options.mutate === true) {
131
+ newS = s;
132
+ }
133
+ if (typeof newS === "object" && Object.keys(definitions).length > 0) {
134
+ newS.definitions = __assign(__assign({}, newS.definitions), definitions);
128
135
  }
129
- return s;
136
+ return newS;
130
137
  }
131
138
  exports.default = referencer;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@json-schema-tools/referencer",
3
- "version": "1.0.6",
3
+ "version": "1.1.1",
4
4
  "main": "build/index.js",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -26,16 +26,19 @@
26
26
  "!build/**/*.test.*"
27
27
  ],
28
28
  "devDependencies": {
29
+ "@json-schema-tools/dereferencer": "^1.6.2",
29
30
  "@json-schema-tools/meta-schema": "^1.7.0",
31
+ "@json-schema-tools/titleizer": "^1.0.8",
32
+ "@json-schema-tools/transpiler": "^1.10.3",
30
33
  "@types/jest": "^29.2.0",
31
- "@types/node": "^18.11.7",
34
+ "@types/node": "^20.12.7",
32
35
  "@typescript-eslint/eslint-plugin": "^5.41.0",
33
36
  "@typescript-eslint/parser": "^5.41.0",
34
37
  "eslint": "^8.26.0",
35
38
  "jest": "^29.2.2",
36
39
  "ts-jest": "^29.0.3",
37
- "typedoc": "^0.23.18",
38
- "typescript": "^4.8.4"
40
+ "typedoc": "^0.25.13",
41
+ "typescript": "^5.4.4"
39
42
  },
40
43
  "dependencies": {
41
44
  "@json-schema-tools/traverse": "^1.10.1"