Registry / devops / json-schema-traverse

json-schema-traverse

JSON →
library1.0.0jsnpmunverified

Traverse JSON Schema objects, calling a callback for each schema node (including the root) in a depth-first pre-order or with separate pre/post callbacks. v1.0.0 is stable and widely used, with no known breaking changes since v0. Present in many JSON Schema validation pipelines (e.g., Ajv). Differentiates from generic object traversal by resolving JSON Pointers and parent schema context. Ships TypeScript declarations. Low release cadence.

npm install json-schema-traverse
INSTALL
IMPORT
SIG · JSON-SCHEMA-TRAVER
J
json-schema-traverse
devopsjavascriptv1.0.0
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

default export (traverse function)
import traverse from 'json-schema-traverse'
const traverse = require('json-schema-traverse')
ESM import works in Node 14+ with "type": "module" or .mjs. CommonJS require is still supported via dynamic import or CJS interop.
type imports (TypeScript)
import type { TraverseCallback, TraverseOptions } from 'json-schema-traverse'
Types are exported but not documented. Use type-only import to avoid runtime overhead.
CommonJS require
const traverse = require('json-schema-traverse')
const { traverse } = require('json-schema-traverse')
The package exports a single function as default. Named destructuring from CJS requires module.exports.default.

Shows basic traversal with callback, pointer collection, and the allKeys option.

import traverse from 'json-schema-traverse'; import type { JSONSchema7 } from 'json-schema'; const schema: JSONSchema7 = { type: 'object', properties: { foo: { type: 'string' }, bar: { type: 'integer' } } }; const results: string[] = []; traverse(schema, { cb: (schema: object, pointer: string) => { results.push(pointer); } }); console.log(results); // ['#', '#/properties/foo', '#/properties/bar'] // With allKeys: true to include non-schema properties const schema2 = { mySchema: { minimum: 1 } }; traverse(schema2, { allKeys: true, cb: (s, ptr) => console.log(ptr) }); // '#', '#/mySchema'
Debug
Known issues
gotchaSchema references ($ref) are not resolved; they are passed as-is.
fix
Use a JSON Schema resolver like json-schema-ref-parser before traversal if you need resolved schemas.
affects: >=0.1.0
gotchaBoolean schemas (draft-06+) are skipped by default; the callback will not be called for `true` or `false` schema values.
fix
To include boolean schemas, set 'allKeys: true'.
affects: >=0.1.0
deprecatedThe option 'cb' as a function is deprecated; pass an object with 'cb' or 'pre'/'post' functions.
fix
Use traverse(schema, { cb: myCallback }) instead of traverse(schema, myCallback).
affects: >=1.0.0
breakingVersion 1.0.0 removed the second argument (post callback) support; now only object form with cb or {pre, post} is accepted.
fix
Migrate from traverse(schema, pre, post) to traverse(schema, { pre, post }).
affects: 1.0.0
Errors
Common errors & fixes
TypeError: traverse is not a function
Using default import with CommonJS require without default export interop.
fix
Use `const traverse = require('json-schema-traverse').default` or enable esModuleInterop in TypeScript.
Traverse callback not called for $ref schemas
$ref nodes are not traversed into by default; they are passed as simple objects.
fix
Use a $ref resolver beforehand, or handle $ref manually in the callback.
Option 'cb' must be a function or object with pre/post
Passing a non-function/non-object as cb option.
fix
Ensure cb is a function or { pre: Function, post: Function }.
Upgrade
Version history
1.0.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
4 hits · last 30 days
node
4
Resources
json-schema-traverse — npm install json-schema-traverse · libregistry