# json-schema-traverse 中文文档

规范地址：https://npmcn.com/package/json-schema-traverse/

版本：1\.0\.0
npm 元数据更新时间：2023-06-22T16:32:38.879Z
中文译文更新时间：2026-11-04T10:41:26.733Z
软件包许可：MIT

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/json-schema-traverse
原文对照：https://npmcn.com/package/json-schema-traverse/?lang=original
项目主页：https://github.com/epoberezkin/json-schema-traverse#readme
源码仓库：https://github.com/epoberezkin/json-schema-traverse

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：22.2 kB，12 个文件
- 模块格式：CommonJS
- TypeScript 类型：包内自带
- 运行依赖：0 个
- 首次发布：2017-06-04T18:42:17.844Z
- 最新版本发布：2020-12-13T10:56:54.719Z
- 近 30 天下载量：1335917777（2026-10-04 至 2026-11-02，npm 官方下载统计）
- 已发布版本数：9（近 12 个月 0 个）

## 中文简介

遍历JSON模式，将每个模式对象传递给回调

## 安装

```sh
npm install json-schema-traverse
```

也可以使用：`pnpm add json-schema-traverse`、`yarn add json-schema-traverse`、`bun add json-schema-traverse`

## README

# json-schema-traverse

遍历JSON模式，将每个模式对象传递给回调

[![build](https://github.com/epoberezkin/json-schema-traverse/workflows/build/badge.svg)](https://github.com/epoberezkin/json-schema-traverse/actions?query=workflow%3Abuild) [![npm](https://img.shields.io/npm/v/json-schema-traverse)](https://www.npmjs.com/package/json-schema-traverse) [![coverage](https://coveralls.io/repos/github/epoberezkin/json-schema-traverse/badge.svg?branch=master)](https://coveralls.io/github/epoberezkin/json-schema-traverse?branch=master)

## 安装

```
npm install json-schema-traverse
```

## 用法

```javascript
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

```

回调函数 `cb` 在预顺序遍历中对每个模式对象（不包括draft-06布尔模式），包括根模式调用。模式引用（$ref）不被解析，它们被原样传递。或者，您可以通过a `{pre, post}` 对象作为 `cb`，然后 `pre` 将在遍历子元素之前调用，并且 `post` 将在遍历所有子元素后调用。

Callback传递这些参数：

-   _模式_：当前模式对象
-   _JSON指针_：从根模式到当前模式对象
-   _根模式_：传递给的模式 `traverse` 对象
-   _父JSON指针_：从根模式到父模式对象（见下文）
-   _父母的关键字_：这个模式出现在其中的关键字(例如： `properties`， `anyOf`等)。
-   _父模式_：不一定是父对象/数组；在上面的示例中，父模式为 `{type: 'string'}` 是根模式
-   _索引/属性_：包含多个模式的数组/对象中的索引或属性名称；在上面的例子中 `{type: 'string'}` 属性名称为 `'foo'`

## 遍历所有未知关键字中的对象

```javascript
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
```

没有选项 `allKeys: true` 回调只会被根模式调用。

## 企业支持

Json-schema-traverse包是 [Tidelift企业订阅](https://tidelift.com/subscription/pkg/npm-json-schema-traverse?utm_source=npm-json-schema-traverse&utm_medium=referral&utm_campaign=enterprise&utm_term=repo) -除了软件维护者提供的支持外，它还为开源软件用户提供集中的商业支持。

## 安全联系

要报告安全漏洞，请使用 [Tidelift安全联系人](https://tidelift.com/security)。 Tidelift将协调修复和披露。请不要通过GitHub问题报告安全漏洞。

## 许可证

[麻省理工学院](https://github.com/epoberezkin/json-schema-traverse/blob/master/LICENSE)

## 常见问题

### json-schema-traverse 是什么？

遍历JSON模式，将每个模式对象传递给回调

### 如何安装 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 和提交记录判断。

