@json-schema-tools/referencer 1.0.6 → 1.1.0
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 +1 -0
- package/build/index.d.ts +10 -1
- package/build/index.js +13 -6
- package/package.json +1 -1
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
|
@@ -64,6 +64,9 @@ var NoTitleError = /** @class */ (function () {
|
|
|
64
64
|
return NoTitleError;
|
|
65
65
|
}());
|
|
66
66
|
exports.NoTitleError = NoTitleError;
|
|
67
|
+
var defaultReferencerOptions = {
|
|
68
|
+
mutate: false,
|
|
69
|
+
};
|
|
67
70
|
/**
|
|
68
71
|
* Returns the schema where all subschemas have been replaced with $refs and added to definitions.
|
|
69
72
|
*
|
|
@@ -79,9 +82,10 @@ exports.NoTitleError = NoTitleError;
|
|
|
79
82
|
* @category SchemaImprover
|
|
80
83
|
*
|
|
81
84
|
*/
|
|
82
|
-
function referencer(s) {
|
|
85
|
+
function referencer(s, options) {
|
|
86
|
+
if (options === void 0) { options = defaultReferencerOptions; }
|
|
83
87
|
var definitions = {};
|
|
84
|
-
(0, traverse_1.default)(s, function (subSchema, isRootCycle) {
|
|
88
|
+
var newS = (0, traverse_1.default)(s, function (subSchema, isRootCycle) {
|
|
85
89
|
var t = "";
|
|
86
90
|
if (isRootCycle && subSchema !== true && subSchema !== false) {
|
|
87
91
|
if (subSchema.$ref) {
|
|
@@ -122,10 +126,13 @@ function referencer(s) {
|
|
|
122
126
|
subSchema.$ref = "#/definitions/".concat(t);
|
|
123
127
|
}
|
|
124
128
|
return subSchema;
|
|
125
|
-
}, { mutable:
|
|
126
|
-
if (
|
|
127
|
-
|
|
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
|
|
136
|
+
return newS;
|
|
130
137
|
}
|
|
131
138
|
exports.default = referencer;
|