@json-schema-tools/traverse
Advanced tools
Comparing version 1.1.2 to 1.2.0
@@ -19,2 +19,7 @@ import { JSONMetaSchema } from "@json-schema-tools/meta-schema"; | ||
mergeNotMutate?: boolean; | ||
/** | ||
* true if you want the original schema that was provided to be directly modified by the provided mutation/merge function | ||
* To preserve cyclical refs this is necessary. | ||
*/ | ||
mutable?: boolean; | ||
} | ||
@@ -21,0 +26,0 @@ export declare const defaultOptions: TraverseOptions; |
@@ -22,2 +22,3 @@ "use strict"; | ||
mergeNotMutate: false, | ||
mutable: false, | ||
}; | ||
@@ -59,3 +60,6 @@ var isCycle = function (s, recursiveStack) { | ||
} | ||
var mutableSchema = __assign({}, schema); | ||
var mutableSchema = schema; | ||
if (traverseOptions.mutable === false) { | ||
mutableSchema = __assign({}, schema); | ||
} | ||
recursiveStack.push(schema); | ||
@@ -62,0 +66,0 @@ prePostMap.push([schema, mutableSchema]); |
@@ -393,8 +393,32 @@ "use strict"; | ||
foo: { | ||
title: "1", | ||
items: [ | ||
$ref: "#" | ||
}, | ||
}, | ||
}; | ||
var result = _1.default(schema, function (s) { | ||
if (s.$ref) { | ||
return schema; | ||
} | ||
return s; | ||
}, { mutable: true }); | ||
var rProps = result.properties; | ||
expect(rProps.foo).toBe(result); | ||
}); | ||
it("handles the mutation function adding a cycle", function () { | ||
var schema = { | ||
title: "1", | ||
type: "object", | ||
properties: { | ||
foo: { | ||
title: "2", | ||
anyOf: [ | ||
{ | ||
title: "0", | ||
title: "3", | ||
type: "array", | ||
items: { title: "2" }, | ||
items: { | ||
title: "4", | ||
properties: { | ||
baz: { title: "5" }, | ||
}, | ||
}, | ||
}, | ||
@@ -405,15 +429,8 @@ ], | ||
}; | ||
schema.properties.foo.items[0].items = schema; // set the leaf to a ref back to root schema | ||
var i = 0; | ||
var result = _1.default(schema, function (s) { | ||
s.i = i; | ||
i += 1; | ||
return s; | ||
}); | ||
var rProps = result.properties; | ||
expect(result.i).toBe(2); | ||
expect(rProps.foo.items[0].i).toBe(0); | ||
expect(rProps.foo.items[0].items.i).toBe(result.i); | ||
schema.properties.foo.anyOf[0].items.properties.baz = schema.properties.foo; | ||
var mockMutation = jest.fn(function (s) { return s; }); | ||
_1.default(schema, mockMutation); | ||
expect(mockMutation).toHaveBeenCalledTimes(4); | ||
}); | ||
}); | ||
}); |
@@ -0,1 +1,13 @@ | ||
# [1.2.0](https://github.com/json-schema-tools/traverse/compare/1.1.2...1.2.0) (2020-06-29) | ||
### Bug Fixes | ||
* use circleci context and parallelize more ([13111d5](https://github.com/json-schema-tools/traverse/commit/13111d5da6ba15921ec9b108c9e34a639b64c223)) | ||
### Features | ||
* add an option that toggles mutating original schema ([#55](https://github.com/json-schema-tools/traverse/issues/55)) ([01506d8](https://github.com/json-schema-tools/traverse/commit/01506d86b8279f93f7da27ea5cbd727f6b246898)) | ||
## [1.1.2](https://github.com/json-schema-tools/traverse/compare/1.1.1...1.1.2) (2020-06-27) | ||
@@ -2,0 +14,0 @@ |
{ | ||
"name": "@json-schema-tools/traverse", | ||
"version": "1.1.2", | ||
"version": "1.2.0", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "build/index.js", |
@@ -450,8 +450,31 @@ import traverse from "./"; | ||
foo: { | ||
title: "1", | ||
items: [ | ||
$ref: "#" | ||
}, | ||
}, | ||
}; | ||
const result: JSONMetaSchema = traverse(schema, (s: JSONMetaSchema) => { | ||
if (s.$ref) { return schema; } | ||
return s; | ||
}, { mutable: true }); | ||
const rProps = result.properties as any; | ||
expect(rProps.foo).toBe(result); | ||
}); | ||
it("handles the mutation function adding a cycle", () => { | ||
const schema = { | ||
title: "1", | ||
type: "object", | ||
properties: { | ||
foo: { | ||
title: "2", | ||
anyOf: [ | ||
{ | ||
title: "0", | ||
title: "3", | ||
type: "array", | ||
items: { title: "2" }, | ||
items: { | ||
title: "4", | ||
properties: { | ||
baz: { title: "5" }, | ||
}, | ||
}, | ||
}, | ||
@@ -462,15 +485,9 @@ ], | ||
}; | ||
schema.properties.foo.items[0].items = schema; // set the leaf to a ref back to root schema | ||
let i = 0; | ||
const result: JSONMetaSchema = traverse(schema, (s: JSONMetaSchema) => { | ||
s.i = i; | ||
i += 1; | ||
return s; | ||
}); | ||
const rProps = result.properties as any; | ||
expect(result.i).toBe(2); | ||
expect(rProps.foo.items[0].i).toBe(0); | ||
expect(rProps.foo.items[0].items.i).toBe(result.i); | ||
schema.properties.foo.anyOf[0].items.properties.baz = schema.properties.foo; | ||
const mockMutation = jest.fn((s) => s); | ||
traverse(schema, mockMutation); | ||
expect(mockMutation).toHaveBeenCalledTimes(4); | ||
}); | ||
}); | ||
}); |
@@ -23,2 +23,8 @@ import merge from "lodash.merge"; | ||
mergeNotMutate?: boolean; | ||
/** | ||
* true if you want the original schema that was provided to be directly modified by the provided mutation/merge function | ||
* To preserve cyclical refs this is necessary. | ||
*/ | ||
mutable?: boolean; | ||
} | ||
@@ -29,2 +35,3 @@ | ||
mergeNotMutate: false, | ||
mutable: false, | ||
}; | ||
@@ -72,3 +79,6 @@ | ||
const mutableSchema: JSONMetaSchema = { ...schema }; | ||
let mutableSchema: JSONMetaSchema = schema; | ||
if (traverseOptions.mutable === false) { | ||
mutableSchema = { ...schema }; | ||
} | ||
recursiveStack.push(schema); | ||
@@ -75,0 +85,0 @@ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
90972
1250