# @anttiviljami/json-schema-ref-parser 中文文档

规范地址：https://npmcn.com/package/%40anttiviljami/json-schema-ref-parser/

版本：9\.2\.2
npm 元数据更新时间：2023-01-25T11:39:53.580Z
中文译文更新时间：2026-11-06T07:13:56.278Z
软件包许可：MIT

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

原始 npm 页面：https://www.npmjs.com/package/%40anttiviljami/json-schema-ref-parser
原文对照：https://npmcn.com/package/%40anttiviljami/json-schema-ref-parser/?lang=original
项目主页：https://apitools.dev/json-schema-ref-parser/
源码仓库：https://github.com/APIDevTools/json-schema-ref-parser

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

- 包体积（解压后）：142 kB，23 个文件
- 模块格式：CommonJS
- TypeScript 类型：包内自带
- 运行依赖：4 个
- 首次发布：2023-01-14T09:35:24.219Z
- 最新版本发布：2023-01-25T11:39:53.477Z
- 近 30 天下载量：13620（2026-10-06 至 2026-11-04，npm 官方下载统计）
- 已发布版本数：3（近 12 个月 0 个）

## 中文简介

解析、解析和引用 JSON Schema $ref 指针

## 安装

```sh
npm install @anttiviljami/json-schema-ref-parser
```

也可以使用：`pnpm add @anttiviljami/json-schema-ref-parser`、`yarn add @anttiviljami/json-schema-ref-parser`、`bun add @anttiviljami/json-schema-ref-parser`

## README

# JSON Schema 参考解析器

_**这个软件包需要 [一个新的维护者](https://github.com/APIDevTools/json-schema-ref-parser/issues/285) 或至少一些贡献者,或相当数量的赞助者,这样我就可以聘请承包商来完成这项工作。 详情 [请阅读本条](https://phil.tech/2022/bundling-openapi-with-javascript/)我会在2023年1月15日 加上一堆其他的@apidevtools 软件包,比如 swagger-parser, 将重播标记为只读且未保存的版本, 这样我就可以专注于扩大我的 [重新造林慈善机构](https://protect.earth/) 而不是把自己烧成灰烬 试图维持一整批OSS项目 我不使用在我的消失的很小的业余时间.** - 说吧 [菲尔·斯通](https://github.com/philsturgeon)_

#### 解析、解析和引用 JSON Schema $ref 指针

[![Build Status](https://github.com/APIDevTools/json-schema-ref-parser/workflows/CI-CD/badge.svg?branch=master)](https://github.com/APIDevTools/json-schema-ref-parser/actions) [![Coverage Status](https://coveralls.io/repos/github/APIDevTools/json-schema-ref-parser/badge.svg?branch=master)](https://coveralls.io/github/APIDevTools/json-schema-ref-parser)

[![npm](https://img.shields.io/npm/v/@apidevtools/json-schema-ref-parser.svg)](https://www.npmjs.com/package/@apidevtools/json-schema-ref-parser) [![Dependencies](https://david-dm.org/APIDevTools/json-schema-ref-parser.svg)](https://david-dm.org/APIDevTools/json-schema-ref-parser) [![License](https://img.shields.io/npm/l/@apidevtools/json-schema-ref-parser.svg)](https://npmcn.com/package/%40anttiviljami/json-schema-ref-parser/LICENSE) [![Buy us a tree](https://img.shields.io/badge/Treeware-%F0%9F%8C%B3-lightgreen)](https://plant.treeware.earth/APIDevTools/json-schema-ref-parser)

[![OS and Browser Compatibility](https://apitools.dev/img/badges/ci-badges-with-ie.svg)](https://github.com/APIDevTools/json-schema-ref-parser/actions)

## 问题:

你有一个JSON斯克马与 `$ref` 指向其它文件和(或)URL. 也许你提前知道所有参考文件 也许你没有。 也许有些是本地文件,有些是远程URL. 也许它们是JSON和YAML格式的混合. 也许有些文件包含相互的交叉引用.

```javascript
{
  "definitions": {
    "person": {
      // references an external file
      "$ref": "schemas/people/Bruce-Wayne.json"
    },
    "place": {
      // references a sub-schema in an external file
      "$ref": "schemas/places.yaml#/definitions/Gotham-City"
    },
    "thing": {
      // references a URL
      "$ref": "http://wayne-enterprises.com/things/batmobile"
    },
    "color": {
      // references a value in an external file via an internal reference
      "$ref": "#/definitions/thing/properties/colors/black-as-the-night"
    }
  }
}
```

## 解决方案:

JSON Schema $Ref Parser 是一个完整的 [JSON 参考](https://tools.ietf.org/html/draft-pbryan-zyp-json-ref-03) 财务报告和审定财务报表 [JSON 指针](https://tools.ietf.org/html/rfc6901) 最复杂的执行 [JSON 谢马斯](http://json-schema.org/latest/json-schema-core.html) 并给你简单直截了当的JavaScript对象.

-   使用 **贾森** 或者说 **也门** 计划,甚至两者的混合!
-   支助 `$ref` 指向外部文件和 URL,以及 [自定义源](https://apitools.dev/json-schema-ref-parser/docs/plugins/resolvers.html) 数据库等
-   可以吗 [捆绑](https://apitools.dev/json-schema-ref-parser/docs/ref-parser.html#bundlepath-options-callback) 多个文件组成一个只有 _内部_ `$ref` 指针
-   可以吗 [调试](https://apitools.dev/json-schema-ref-parser/docs/ref-parser.html#dereferencepath-options-callback) 生成一个简单易行的 JavaScript 对象
-   支助 [参考文献](https://apitools.dev/json-schema-ref-parser/docs/#circular-refs),嵌入式引用,回溯引用,以及文件之间的交叉引用
-   保持对象引用平等 - `$ref` 指向同一值的总是对同一对象的解析
-   在Node v10, v12, & v14中测试,并在Windows, Mac和Linux上测试了所有主要网页浏览器

## 示例

```javascript
$RefParser.dereference(mySchema, (err, schema) => {
  if (err) {
    console.error(err);
  }
  else {
    // `schema` is just a normal JavaScript object that contains your entire JSON Schema,
    // including referenced files, combined into a single object
    console.log(schema.definitions.person.properties.firstName);
  }
})
```

或使用 `async`编号 :`await` 换成语法. 下例与上同

```javascript
try {
  let schema = await $RefParser.dereference(mySchema);
  console.log(schema.definitions.person.properties.firstName);
}
catch(err) {
  console.error(err);
}
```

详情请参见 [API 文档](https://apitools.dev/json-schema-ref-parser/docs/)

## 安装

使用 [无](https://docs.npmjs.com/about-npm/)数字 :

```bash
npm install @apidevtools/json-schema-ref-parser
```

## 使用量

当在 Node.js apps 中使用 JSON Schema $Ref Parser 时,您可能想要使用 **常见JS** 语法 :

```javascript
const $RefParser = require("@apidevtools/json-schema-ref-parser");
```

当使用诸如 [宝贝](https://babeljs.io/) 或者说 [类型脚本](https://www.typescriptlang.org/),或诸如 [网络软件包](https://webpack.js.org/) 或者说 [滚动](https://rollupjs.org/),您可以使用 **ECMAScript 模块** 语法代替 :

```javascript
import $RefParser from "@apidevtools/json-schema-ref-parser";
```

如果您使用Node.js < 18, 您需要多填充 `fetch`一样 [节点比对](https://github.com/node-fetch/node-fetch)数字 :

```javascript
import fetch from "node-fetch";

globalThis.fetch = fetch;
```

## 浏览器支持

JSON Schema $Ref Parser支持每个主要网页浏览器的最近版本. 旧浏览器可能需要 [宝贝](https://babeljs.io/) 和(或) [多元填充器](https://babeljs.io/docs/en/next/babel-polyfill)。 。 。 .

要在浏览器中使用 JSON Schema $Ref Parser, 您需要使用捆绑工具, 如 [网络软件包](https://webpack.js.org/), (中文(简体) ), [滚动](https://rollupjs.org/), (中文(简体) ), [括号](https://parceljs.org/),或 [浏览器](http://browserify.org/)。有些捆绑者可能需要一些配置,例如设置 `browser: true` 内 [卷起插件溶解](https://github.com/rollup/rollup-plugin-node-resolve)。 。 。 .

## API 文档

完备的 API 文档 [就在这里](https://apitools.dev/json-schema-ref-parser/docs/)

## 捐款

我欢迎任何贡献、增强和错误修正. [打开一个问题](https://github.com/APIDevTools/json-schema-ref-parser/issues) 关于 GitHub 和 [提交拉动请求](https://github.com/APIDevTools/json-schema-ref-parser/pulls)。 。 。 .

#### 建筑物/试验

要在您的计算机上本地构建/测试项目:

1.  **克隆这个还原**  
    `git clone https://github.com/APIDevTools/json-schema-ref-parser.git`
    
2.  **安装依赖关系**  
    `npm install`
    
3.  **运行测试**  
    `npm test`
    

## 许可证

JSON Schema $Ref Parser 是100%的自由和开放源代码,在 [麻省理工学院许可证](https://npmcn.com/package/%40anttiviljami/json-schema-ref-parser/LICENSE)随意使用.

这个软件包是 [树件](http://treeware.earth/)如果你把它用于生产,那么我们要求你 [**给世界买一棵树**](https://plant.treeware.earth/APIDevTools/json-schema-ref-parser) 感谢我们的工作。 将会为当地家庭创造就业机会.

## 谢谢

感谢这些优秀的公司对开源开发商的支持 QQ

[![Stoplight](https://svgshare.com/i/TK5.svg)](https://stoplight.io/?utm_source=github&utm_medium=readme&utm_campaign=json_schema_ref_parser) [![SauceLabs](https://jstools.dev/img/badges/sauce-labs.svg)](https://saucelabs.com/) [![Coveralls](https://jstools.dev/img/badges/coveralls.svg)](https://coveralls.io/)

## 常见问题

### @anttiviljami/json-schema-ref-parser 是什么？

解析、解析和引用 JSON Schema $ref 指针

### 如何安装 @anttiviljami/json-schema-ref-parser？

在项目目录运行 npm install @anttiviljami/json-schema-ref-parser，或使用 pnpm add / yarn add / bun add。

### @anttiviljami/json-schema-ref-parser 支持 TypeScript 吗？

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

### @anttiviljami/json-schema-ref-parser 支持 ESM 还是 CommonJS？

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

### @anttiviljami/json-schema-ref-parser 可以商用吗？

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

### @anttiviljami/json-schema-ref-parser 的下载量有多少？

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

### @anttiviljami/json-schema-ref-parser 还在维护吗？

最新版本 9\.2\.2 发布于 2023/01/25（3 年前）；至今共发布 3 个版本，其中过去 12 个月发布了 0 个；首次发布于 2023/01/14。发布频率只是参考信号：功能稳定的工具可能很少需要更新，建议结合源码仓库的 issue 和提交记录判断。

