@json-schema-tools/traverse
Advanced tools
Comparing version 1.6.0 to 1.7.0
@@ -7,4 +7,5 @@ import { JSONSchema } from "@json-schema-tools/meta-schema"; | ||
* @param isRootOfCycle false if the schema passed is not the root of a detected cycle. Useful for special handling of cycled schemas. | ||
* @param path json path string separated by periods | ||
*/ | ||
export declare type MutationFunction = (schema: JSONSchema, isRootOfCycle: boolean) => JSONSchema; | ||
export declare type MutationFunction = (schema: JSONSchema, isRootOfCycle: boolean, path: string) => JSONSchema; | ||
/** | ||
@@ -46,2 +47,2 @@ * The options you can use when traversing. | ||
*/ | ||
export default function traverse(schema: JSONSchema, mutation: MutationFunction, traverseOptions?: TraverseOptions, depth?: number, recursiveStack?: JSONSchema[], prePostMap?: Array<[JSONSchema, JSONSchema]>): JSONSchema; | ||
export default function traverse(schema: JSONSchema, mutation: MutationFunction, traverseOptions?: TraverseOptions, depth?: number, recursiveStack?: JSONSchema[], pathStack?: string[], prePostMap?: Array<[JSONSchema, JSONSchema]>): JSONSchema; |
@@ -13,2 +13,9 @@ "use strict"; | ||
}; | ||
var __spreadArrays = (this && this.__spreadArrays) || function () { | ||
for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length; | ||
for (var r = Array(s), k = 0, i = 0; i < il; i++) | ||
for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++) | ||
r[k] = a[j]; | ||
return r; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
@@ -21,2 +28,5 @@ exports.defaultOptions = void 0; | ||
}; | ||
var jsonPathStringify = function (s) { | ||
return s.map(function (i) { return i === "" ? i.toString() : ("/" + i.toString()); }).join(""); | ||
}; | ||
var isCycle = function (s, recursiveStack) { | ||
@@ -40,6 +50,7 @@ var foundInRecursiveStack = recursiveStack.find(function (recSchema) { return recSchema === s; }); | ||
*/ | ||
function traverse(schema, mutation, traverseOptions, depth, recursiveStack, prePostMap) { | ||
function traverse(schema, mutation, traverseOptions, depth, recursiveStack, pathStack, prePostMap) { | ||
if (traverseOptions === void 0) { traverseOptions = exports.defaultOptions; } | ||
if (depth === void 0) { depth = 0; } | ||
if (recursiveStack === void 0) { recursiveStack = []; } | ||
if (pathStack === void 0) { pathStack = []; } | ||
if (prePostMap === void 0) { prePostMap = []; } | ||
@@ -52,2 +63,5 @@ var isRootOfCycle = false; | ||
// boolean is seen in a further nested schema. | ||
if (depth === 0) { | ||
pathStack = [""]; | ||
} | ||
if (typeof schema === "boolean" || schema instanceof Boolean) { | ||
@@ -58,3 +72,3 @@ if (opts.skipFirstMutation === true && depth === 0) { | ||
else { | ||
return mutation(schema, false); | ||
return mutation(schema, false, jsonPathStringify(pathStack)); | ||
} | ||
@@ -68,3 +82,3 @@ } | ||
if (opts.skipFirstMutation === false || depth !== 0) { | ||
mutableSchema = mutation(mutableSchema, false); | ||
mutableSchema = mutation(mutableSchema, false, jsonPathStringify(pathStack)); | ||
} | ||
@@ -74,3 +88,3 @@ } | ||
prePostMap.push([schema, mutableSchema]); | ||
var rec = function (s) { | ||
var rec = function (s, path) { | ||
var foundCycle = isCycle(s, recursiveStack); | ||
@@ -84,3 +98,3 @@ if (foundCycle) { | ||
if (opts.skipFirstMutation === true && foundCycle === recursiveStack[0]) { | ||
return mutation(s, true); | ||
return mutation(s, true, jsonPathStringify(path)); | ||
} | ||
@@ -93,12 +107,21 @@ var _a = prePostMap.find(function (_a) { | ||
} | ||
return traverse(s, mutation, traverseOptions, depth + 1, recursiveStack, prePostMap); | ||
return traverse(s, mutation, traverseOptions, depth + 1, recursiveStack, path, prePostMap); | ||
}; | ||
if (schema.anyOf) { | ||
mutableSchema.anyOf = schema.anyOf.map(rec); | ||
mutableSchema.anyOf = schema.anyOf.map(function (x, i) { | ||
var result = rec(x, __spreadArrays(pathStack, ["anyOf", i.toString()])); | ||
return result; | ||
}); | ||
} | ||
else if (schema.allOf) { | ||
mutableSchema.allOf = schema.allOf.map(rec); | ||
mutableSchema.allOf = schema.allOf.map(function (x, i) { | ||
var result = rec(x, __spreadArrays(pathStack, ["allOf", i.toString()])); | ||
return result; | ||
}); | ||
} | ||
else if (schema.oneOf) { | ||
mutableSchema.oneOf = schema.oneOf.map(rec); | ||
mutableSchema.oneOf = schema.oneOf.map(function (x, i) { | ||
var result = rec(x, __spreadArrays(pathStack, ["oneOf", i.toString()])); | ||
return result; | ||
}); | ||
} | ||
@@ -109,3 +132,6 @@ else { | ||
if (schema.items instanceof Array) { | ||
mutableSchema.items = schema.items.map(rec); | ||
mutableSchema.items = schema.items.map(function (x, i) { | ||
var result = rec(x, __spreadArrays(pathStack, ["items", i.toString()])); | ||
return result; | ||
}); | ||
} | ||
@@ -119,3 +145,3 @@ else { | ||
if (opts.skipFirstMutation === true && foundCycle_1 === recursiveStack[0]) { | ||
mutableSchema.items = mutation(schema.items, true); | ||
mutableSchema.items = mutation(schema.items, true, jsonPathStringify(pathStack)); | ||
} | ||
@@ -132,3 +158,3 @@ else { | ||
itemsIsSingleSchema = true; | ||
mutableSchema.items = traverse(schema.items, mutation, traverseOptions, depth + 1, recursiveStack, prePostMap); | ||
mutableSchema.items = traverse(schema.items, mutation, traverseOptions, depth + 1, recursiveStack, pathStack, prePostMap); | ||
} | ||
@@ -138,3 +164,3 @@ } | ||
if (schema.additionalItems !== undefined && !!schema.additionalItems === true && !itemsIsSingleSchema) { | ||
mutableSchema.additionalItems = rec(schema.additionalItems); | ||
mutableSchema.additionalItems = rec(schema.additionalItems, __spreadArrays(pathStack, ["additionalItems"])); | ||
} | ||
@@ -145,3 +171,3 @@ if (schema.properties !== undefined) { | ||
Object.keys(schema.properties).forEach(function (schemaPropKey) { | ||
mutableProps_1[schemaPropKey] = rec(sProps_1[schemaPropKey]); | ||
mutableProps_1[schemaPropKey] = rec(sProps_1[schemaPropKey], __spreadArrays(pathStack, ["properties", schemaPropKey.toString()])); | ||
}); | ||
@@ -154,3 +180,3 @@ mutableSchema.properties = mutableProps_1; | ||
Object.keys(schema.patternProperties).forEach(function (regex) { | ||
mutableProps_2[regex] = rec(sProps_2[regex]); | ||
mutableProps_2[regex] = rec(sProps_2[regex], __spreadArrays(pathStack, ["patternProperties", regex.toString()])); | ||
}); | ||
@@ -160,3 +186,3 @@ mutableSchema.patternProperties = mutableProps_2; | ||
if (schema.additionalProperties !== undefined && !!schema.additionalProperties === true) { | ||
mutableSchema.additionalProperties = rec(schema.additionalProperties); | ||
mutableSchema.additionalProperties = rec(schema.additionalProperties, __spreadArrays(pathStack, ["additionalProperties"])); | ||
} | ||
@@ -171,5 +197,5 @@ } | ||
else { | ||
return mutation(mutableSchema, isRootOfCycle); | ||
return mutation(mutableSchema, isRootOfCycle, jsonPathStringify(pathStack)); | ||
} | ||
} | ||
exports.default = traverse; |
@@ -0,1 +1,29 @@ | ||
# [1.7.0](https://github.com/json-schema-tools/traverse/compare/1.6.0...1.7.0) (2020-09-21) | ||
### Bug Fixes | ||
* add space ([9a7e27e](https://github.com/json-schema-tools/traverse/commit/9a7e27ec155e21c79a2d6d14b05b18c51efdd288)) | ||
* added array pathing ([936105a](https://github.com/json-schema-tools/traverse/commit/936105a624c8bb3d3fb5d30d13fce0013139cb2d)) | ||
* added array pathing ([9b6d723](https://github.com/json-schema-tools/traverse/commit/9b6d7233078e23caaddbf90545aafe7fa5576c16)) | ||
* change from JSON-Path spec to RFC 6901 for path output ([768bfca](https://github.com/json-schema-tools/traverse/commit/768bfcaa60ed4f56af28257cfd9548e7ed4a6f0d)) | ||
* change from JSON-Path spec to RFC 6901 for path output ([ccd369a](https://github.com/json-schema-tools/traverse/commit/ccd369a2de236a1993d7d55d6c6910c41ad161bd)) | ||
* clean up stringify code ([a639c35](https://github.com/json-schema-tools/traverse/commit/a639c35d7d5c304cb00512544911829fcd477aff)) | ||
* clean up stringify code ([bd28424](https://github.com/json-schema-tools/traverse/commit/bd284248aedf7c2b9ad649e57162e6c60ca7b029)) | ||
* remove implicit typecasting ([e534193](https://github.com/json-schema-tools/traverse/commit/e53419385db316e4cd52bc441b15b8d9586cec3a)) | ||
* remove merge artifact ([858ef35](https://github.com/json-schema-tools/traverse/commit/858ef35f282a0060d16223abc3d8109956f779a0)) | ||
* remove push/pop to improve performance ([9bf5eff](https://github.com/json-schema-tools/traverse/commit/9bf5effdda9acf2ce5a806129d70b2302473dd87)) | ||
* standardize pathstack to string array ([b36ef4a](https://github.com/json-schema-tools/traverse/commit/b36ef4a6155fd032b4dbdf3c1b650d9c14d94e42)) | ||
* standardize pathstack to string array ([48550f7](https://github.com/json-schema-tools/traverse/commit/48550f714270b393a6e3f60b5c44331f94d21633)) | ||
* update index.test.ts to use path ([7d9cedf](https://github.com/json-schema-tools/traverse/commit/7d9cedfc9caafa6952b1d6bd5472aeb369fab50b)) | ||
* update index.test.ts to use path ([5bc96db](https://github.com/json-schema-tools/traverse/commit/5bc96dbfa3a023acb5bb7424a635b3057df3f187)) | ||
* update mutation function args and delete obsolete test file ([3fb1bfd](https://github.com/json-schema-tools/traverse/commit/3fb1bfdf84afd89cd3249e5dc7629cf0779b1c8c)) | ||
* update mutation function args and delete obsolete test file ([4607ae1](https://github.com/json-schema-tools/traverse/commit/4607ae1faa1a2fac5a3d311f61139991655c5be5)) | ||
### Features | ||
* add path to mutator function ([85551ab](https://github.com/json-schema-tools/traverse/commit/85551ab7165b4a02b8b5c3b15baafa64abc9cc31)) | ||
* add path to mutator function ([e9137d3](https://github.com/json-schema-tools/traverse/commit/e9137d350e1a4d4c2f5ab51be3793c69f2a0f7d2)) | ||
# [1.6.0](https://github.com/json-schema-tools/traverse/compare/1.5.0...1.6.0) (2020-08-24) | ||
@@ -2,0 +30,0 @@ |
{ | ||
"name": "@json-schema-tools/traverse", | ||
"version": "1.6.0", | ||
"version": "1.7.0", | ||
"description": "This package exports a method that will traverse a JSON-Schema, calling a mutation function for each sub schema found. It is useful for building tools to work with JSON Schemas.", | ||
@@ -35,5 +35,5 @@ "main": "build/index.js", | ||
"ts-jest": "^24.3.0", | ||
"typedoc": "^0.17.7", | ||
"typedoc": "^0.19.1", | ||
"typescript": "^3.9.5" | ||
} | ||
} |
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
34209
230