json-schema-traverse

中文文档
1.0.0发布于 2020/12/13MITTypeScriptCommonJS零依赖近 30 天下载 13.4亿

Traverse JSON Schema passing each schema object to callback

原始使用文档
文档目录6 节
  1. Install
  2. Usage
  3. Traverse objects in all unknown keywords
  4. Enterprise support
  5. Security contact
  6. License

json-schema-traverse

Traverse JSON Schema passing each schema object to callback

build npm coverage

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});
// cb is called 3 times with:
// 1. root schema
// 2. {type: 'string'}
// 3. {type: 'integer'}

// Or:

traverse(schema, {cb: {pre, post}});
// pre is called 3 times with:
// 1. root schema
// 2. {type: 'string'}
// 3. {type: 'integer'}
//
// post is called 3 times with:
// 1. {type: 'string'}
// 2. {type: 'integer'}
// 3. root schema

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});
// cb is called 2 times with:
// 1. root schema
// 2. mySchema

Without option allKeys: true callback will be called only with root schema.

Enterprise support

json-schema-traverse package is a part of Tidelift enterprise subscription - it provides a centralised commercial support to open-source software users, in addition to the support provided by software maintainers.

Security contact

To report a security vulnerability, please use the Tidelift security contact. Tidelift will coordinate the fix and disclosure. Please do NOT report security vulnerability via GitHub issues.

License

MIT

版本与发布记录

json-schema-traverse 共发布 9 个版本,首次发布于 2017/06/05;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.0.0latest2020/12/135 年前
0.5.02020/09/066 年前
0.4.12018/06/108 年前
0.4.02018/05/098 年前
0.3.12017/06/249 年前
0.3.02017/06/169 年前

关于 json-schema-traverse 的常见问题

json-schema-traverse 是什么?

Traverse JSON Schema passing each schema object to callback

如何安装 json-schema-traverse?

在项目目录运行 npm install json-schema-traverse,或使用 pnpm add / yarn add / bun add。

json-schema-traverse 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

json-schema-traverse 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

json-schema-traverse 可以商用吗?

json-schema-traverse 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

json-schema-traverse 的下载量有多少?

根据 npm 官方下载统计,json-schema-traverse 在 2026/10/04 – 2026/11/02 这 30 天内被下载 1,335,917,777 次,日均约 44,530,593 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

json-schema-traverse 还在维护吗?

最新版本 1.0.0 发布于 2020/12/13(5 年前);至今共发布 9 个版本,其中过去 12 个月发布了 0 个;首次发布于 2017/06/05。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。