@json-schema-tools/traverse
Advanced tools
Comparing version 1.3.1 to 1.4.0
@@ -1,2 +0,2 @@ | ||
import { JSONMetaSchema } from "@json-schema-tools/meta-schema"; | ||
import { JSONSchema } from "@json-schema-tools/meta-schema"; | ||
/** | ||
@@ -8,3 +8,3 @@ * Signature of the mutation method passed to traverse. | ||
*/ | ||
export declare type MutationFunction = (schema: JSONMetaSchema, isRootOfCycle: boolean) => JSONMetaSchema; | ||
export declare type MutationFunction = (schema: JSONSchema, isRootOfCycle: boolean) => JSONSchema; | ||
/** | ||
@@ -41,2 +41,2 @@ * The options you can use when traversing. | ||
*/ | ||
export default function traverse(schema: JSONMetaSchema, mutation: MutationFunction, traverseOptions?: TraverseOptions, depth?: number, recursiveStack?: JSONMetaSchema[], prePostMap?: Array<[JSONMetaSchema, JSONMetaSchema]>): JSONMetaSchema; | ||
export default function traverse(schema: JSONSchema, mutation: MutationFunction, traverseOptions?: TraverseOptions, depth?: number, recursiveStack?: JSONSchema[], prePostMap?: Array<[JSONSchema, JSONSchema]>): JSONSchema; |
@@ -118,14 +118,12 @@ "use strict"; | ||
} | ||
if (!!schema.additionalItems === true && !itemsIsSingleSchema) { | ||
if (schema.additionalItems !== undefined && !!schema.additionalItems === true && !itemsIsSingleSchema) { | ||
mutableSchema.additionalItems = rec(schema.additionalItems); | ||
} | ||
if (schema.properties) { | ||
if (schema.properties !== undefined) { | ||
var sProps_1 = schema.properties; | ||
mutableSchema.properties = Object.keys(sProps_1) | ||
.reduce(function (r, v) { | ||
var _a; | ||
return (__assign(__assign({}, r), (_a = {}, _a[v] = rec(sProps_1[v]), _a))); | ||
}, {}); | ||
Object.keys(schema.properties).forEach(function (schemaPropKey) { | ||
mutableSchema.properties[schemaPropKey] = rec(sProps_1[schemaPropKey]); | ||
}); | ||
} | ||
if (!!schema.additionalProperties === true) { | ||
if (schema.additionalProperties !== undefined && !!schema.additionalProperties === true) { | ||
mutableSchema.additionalProperties = rec(schema.additionalProperties); | ||
@@ -132,0 +130,0 @@ } |
@@ -0,1 +1,14 @@ | ||
# [1.4.0](https://github.com/json-schema-tools/traverse/compare/1.3.1...1.4.0) (2020-07-21) | ||
### Bug Fixes | ||
* set node version ([24b9516](https://github.com/json-schema-tools/traverse/commit/24b9516b1cf27560d54c4c1fba647524f9bf38c3)) | ||
* test are now passing ([65ca712](https://github.com/json-schema-tools/traverse/commit/65ca7128301a29cbca5eae54c8aaeeb10c8c0854)) | ||
### Features | ||
* update to new meta-schema ([82a133e](https://github.com/json-schema-tools/traverse/commit/82a133ecc0825f1ab989e5c28b75451dcd887c74)) | ||
## [1.3.1](https://github.com/json-schema-tools/traverse/compare/1.3.0...1.3.1) (2020-07-20) | ||
@@ -2,0 +15,0 @@ |
{ | ||
"name": "@json-schema-tools/traverse", | ||
"version": "1.3.1", | ||
"description": "", | ||
"version": "1.4.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.", | ||
"main": "build/index.js", | ||
@@ -14,6 +14,6 @@ "publishConfig": { | ||
}, | ||
"author": "", | ||
"author": "Zachary Belford<belfordz66@gmail.com>", | ||
"license": "Apache-2.0", | ||
"devDependencies": { | ||
"@json-schema-tools/meta-schema": "^1.0.10", | ||
"@json-schema-tools/meta-schema": "^1.3.0", | ||
"@types/jest": "^26.0.3", | ||
@@ -20,0 +20,0 @@ "@types/lodash.merge": "^4.6.6", |
@@ -1,3 +0,3 @@ | ||
import traverse from "./"; | ||
import { JSONMetaSchema, Properties } from "@json-schema-tools/meta-schema"; | ||
import traverse, { MutationFunction } from "./"; | ||
import { Properties, JSONSchemaObject, JSONSchema } from "@json-schema-tools/meta-schema"; | ||
@@ -20,3 +20,3 @@ describe("traverse", () => { | ||
const result = traverse(testSchema, mutator) as JSONMetaSchema; | ||
const result = traverse(testSchema, mutator) as any; | ||
@@ -32,3 +32,3 @@ expect(result.hello).toBe("world"); | ||
const mutator = jest.fn((s: JSONMetaSchema) => ({ | ||
const mutator = jest.fn((s: JSONSchemaObject) => ({ | ||
...s, | ||
@@ -40,3 +40,3 @@ properties: { | ||
const result = traverse(testSchema, mutator) as JSONMetaSchema; | ||
const result = traverse(testSchema, mutator as MutationFunction) as JSONSchemaObject; | ||
@@ -52,3 +52,3 @@ expect(result.properties).toBeDefined(); | ||
}; | ||
const mergeProducer = jest.fn((s: JSONMetaSchema) => ({ | ||
const mergeProducer = jest.fn((s: JSONSchemaObject) => ({ | ||
...s, | ||
@@ -62,3 +62,3 @@ properties: { | ||
const result = traverse(testSchema, mergeProducer, opts) as JSONMetaSchema; | ||
const result = traverse(testSchema, mergeProducer as MutationFunction, opts) as JSONSchemaObject; | ||
@@ -428,7 +428,8 @@ expect(result.properties).toBeDefined(); | ||
}; | ||
const result: JSONMetaSchema = traverse(schema, (s: JSONMetaSchema) => { | ||
if (s.$ref) { return schema; } | ||
const result = traverse(schema, (s: JSONSchema) => { | ||
if ((s as JSONSchemaObject).$ref) { return schema; } | ||
return s; | ||
}, { mutable: true }); | ||
const rProps = result.properties as any; | ||
}, { mutable: true }) as JSONSchemaObject; | ||
const rProps = result.properties as Properties; | ||
expect(rProps.foo).toBe(result); | ||
@@ -507,6 +508,6 @@ }); | ||
const mockMutation1 = jest.fn((mockS) => mockS); | ||
const testSchema1Result = traverse(testSchema1, mockMutation1, { skipFirstMutation: true }) as JSONMetaSchema; | ||
const testSchema1Result = traverse(testSchema1, mockMutation1, { skipFirstMutation: true }) as JSONSchemaObject; | ||
const mockMutation2 = jest.fn((mockS) => mockS); | ||
const testSchema2Result = traverse(testSchema2, mockMutation2, { skipFirstMutation: true }) as JSONMetaSchema; | ||
const testSchema2Result = traverse(testSchema2, mockMutation2, { skipFirstMutation: true }) as JSONSchemaObject; | ||
@@ -513,0 +514,0 @@ expect(mockMutation1).toHaveBeenCalledWith(testSchema1, true); |
@@ -1,2 +0,2 @@ | ||
import { JSONMetaSchema } from "@json-schema-tools/meta-schema"; | ||
import { JSONSchema, JSONSchemaObject, Properties } from "@json-schema-tools/meta-schema"; | ||
@@ -9,3 +9,3 @@ /** | ||
*/ | ||
export type MutationFunction = (schema: JSONMetaSchema, isRootOfCycle: boolean) => JSONMetaSchema; | ||
export type MutationFunction = (schema: JSONSchema, isRootOfCycle: boolean) => JSONSchema; | ||
@@ -39,3 +39,3 @@ /** | ||
const isCycle = (s: JSONMetaSchema, recursiveStack: JSONMetaSchema[]): JSONMetaSchema | false => { | ||
const isCycle = (s: JSONSchema, recursiveStack: JSONSchema[]): JSONSchema | false => { | ||
const foundInRecursiveStack = recursiveStack.find((recSchema) => recSchema === s); | ||
@@ -60,9 +60,9 @@ if (foundInRecursiveStack) { | ||
export default function traverse( | ||
schema: JSONMetaSchema, | ||
schema: JSONSchema, | ||
mutation: MutationFunction, | ||
traverseOptions = defaultOptions, | ||
depth = 0, | ||
recursiveStack: JSONMetaSchema[] = [], | ||
prePostMap: Array<[JSONMetaSchema, JSONMetaSchema]> = [], | ||
): JSONMetaSchema { | ||
recursiveStack: JSONSchema[] = [], | ||
prePostMap: Array<[JSONSchema, JSONSchema]> = [], | ||
): JSONSchema { | ||
let isRootOfCycle = false; | ||
@@ -82,3 +82,3 @@ | ||
let mutableSchema: JSONMetaSchema = schema; | ||
let mutableSchema: JSONSchemaObject = schema; | ||
if (traverseOptions.mutable === false) { | ||
@@ -92,3 +92,3 @@ mutableSchema = { ...schema }; | ||
const rec = (s: JSONMetaSchema): JSONMetaSchema => { | ||
const rec = (s: JSONSchema): JSONSchema => { | ||
const foundCycle = isCycle(s, recursiveStack); | ||
@@ -106,3 +106,3 @@ if (foundCycle) { | ||
([orig]) => foundCycle === orig, | ||
) as [JSONMetaSchema, JSONMetaSchema]; | ||
) as [JSONSchema, JSONSchema]; | ||
@@ -144,3 +144,3 @@ return cycledMutableSchema; | ||
([orig]) => foundCycle === orig, | ||
) as [JSONMetaSchema, JSONMetaSchema]; | ||
) as [JSONSchema, JSONSchema]; | ||
@@ -163,16 +163,15 @@ mutableSchema.items = cycledMutableSchema; | ||
if (!!schema.additionalItems === true && !itemsIsSingleSchema) { | ||
if (schema.additionalItems !== undefined && !!schema.additionalItems === true && !itemsIsSingleSchema) { | ||
mutableSchema.additionalItems = rec(schema.additionalItems); | ||
} | ||
if (schema.properties) { | ||
const sProps: { [key: string]: JSONMetaSchema } = schema.properties; | ||
mutableSchema.properties = Object.keys(sProps) | ||
.reduce( | ||
(r: JSONMetaSchema, v: string) => ({ ...r, ...{ [v]: rec(sProps[v]) } }), | ||
{}, | ||
); | ||
if (schema.properties !== undefined) { | ||
const sProps: { [key: string]: JSONSchema } = schema.properties; | ||
Object.keys(schema.properties).forEach((schemaPropKey: string) => { | ||
(mutableSchema.properties as Properties)[schemaPropKey] = rec(sProps[schemaPropKey]); | ||
}); | ||
} | ||
if (!!schema.additionalProperties === true) { | ||
if (schema.additionalProperties !== undefined && !!schema.additionalProperties === true) { | ||
mutableSchema.additionalProperties = rec(schema.additionalProperties); | ||
@@ -179,0 +178,0 @@ } |
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
No contributors or author data
MaintenancePackage does not specify a list of contributors or an author in package.json.
Found 1 instance in 1 package
100529
28
1
1364