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.
- circular reference detection & handling
- synchronous - doesn't touch the filesystem or make network requests.
- easily perform schema mutations while traversing
- optional mutability (toggle updating original schema object)
- returns JSONPaths as it traverses
npm install @json-schema-tools/traverse
const traverse = require("@json-schema-tools/traverse").default;
//import traverse from "@json-schema-tools/traverse"
const mySchema = {
title: "baz",
type: "object",
properties: {
foo: {
title: "foo",
type: "array",
items: { type: "string" }
},
bar: {
title: "bar",
anyOf: [
{ title: "stringerific", type: "string" },
{ title: "numberoo", type: "number" }
]
}
}
};
traverse(mySchema, (schemaOrSubschema) => {
console.log(schemaOrSubschema.title);
});
traverse
accepts an optional options object as the third argument. Some useful
flags include:
bfs
- process schemas in a breadth first orderskipFirstMutation
- do not call the mutation function on the root schemamergeNotMutate
- merge the mutation result back into the original schema
traverse(mySchema, (schemaOrSubschema) => {
console.log(schemaOrSubschema.title);
}, {
bfs: true,
skipFirstMutation: true,
mergeNotMutate: true,
});
The full TypeDoc generated API documentation is available at https://json-schema-tools.github.io/traverse/.
How to contribute, build and release are outlined in CONTRIBUTING.md, BUILDING.md and RELEASING.md respectively. Commits in this repository follow the CONVENTIONAL_COMMITS.md specification.