What is json-schema-traverse?
The json-schema-traverse package is used to traverse and process JSON Schemas. It allows you to visit every schema node to analyze or modify the schema. This can be useful for schema analysis, transformation, and enhancement.
What are json-schema-traverse's main functionalities?
Traversing a JSON Schema
This feature allows you to traverse a JSON Schema and execute a callback function at each node. The callback receives information about the current schema node, its path, and its context within the overall schema.
{"schema": {"type": "object", "properties": {"foo": {"type": "string"}, "bar": {"type": "number"}}}, "callback": "(schema, jsonPtr, rootSchema, parentJsonPtr, parentKeyword, parentSchema, keyIndex) => { console.log('Traversing:', jsonPtr); }"}
Modifying a JSON Schema during traversal
This feature allows you to modify the schema nodes during traversal. For example, you can add constraints like 'minLength' to string properties.
{"schema": {"type": "object", "properties": {"foo": {"type": "string"}, "bar": {"type": "number"}}}, "callback": "(schema, jsonPtr, rootSchema, parentJsonPtr, parentKeyword, parentSchema, keyIndex) => { if (schema.type === 'string') { schema.minLength = 1; } }"}
Other packages similar to json-schema-traverse
json-schema-walker
json-schema-walker is similar to json-schema-traverse in that it allows you to walk through a JSON Schema. However, it has a different API and may offer different customization options for the traversal process.
json-schema-ref-parser
json-schema-ref-parser can dereference JSON Schemas, resolving all $ref pointers. This is different from json-schema-traverse, which does not resolve references but can be used in conjunction with it to traverse the dereferenced schema.
json-schema-traverse
Traverse JSON Schema passing each schema object to callback
Install
npm install json-schema-traverse
Usage
const traverse = require('json-schema-traverse');
const schema = {
properties: {
foo: {type: 'string'},
bar: {type: 'integer'}
}
};
traverse(schema, {cb});
traverse(schema, {cb: {pre, post}});
Callback function cb
is called for each schema object (not including draft-06 boolean schemas), including the root schema, in pre-order traversal. Schema references ($ref) are not resolved, they are passed as is. Alternatively, you can pass a {pre, post}
object as cb
, and then pre
will be called before traversing child elements, and post
will be called after all child elements have been traversed.
Callback is passed these parameters:
- schema: the current schema object
- JSON pointer: from the root schema to the current schema object
- root schema: the schema passed to
traverse
object - parent JSON pointer: from the root schema to the parent schema object (see below)
- parent keyword: the keyword inside which this schema appears (e.g.
properties
, anyOf
, etc.) - parent schema: not necessarily parent object/array; in the example above the parent schema for
{type: 'string'}
is the root schema - index/property: index or property name in the array/object containing multiple schemas; in the example above for
{type: 'string'}
the property name is 'foo'
Traverse objects in all unknown keywords
const traverse = require('json-schema-traverse');
const schema = {
mySchema: {
minimum: 1,
maximum: 2
}
};
traverse(schema, {allKeys: true, cb});
Without option allKeys: true
callback will be called only with root schema.
License
MIT