@bcherny/json-schema-ref-parser

中文文档
10.0.5-fork发布于 2023/02/23MITTypeScriptESM + CJS近 30 天下载 172.9万

Parse, Resolve, and Dereference JSON Schema $ref pointers

原始使用文档
文档目录10 节
  1. The Problem:
  2. The Solution:
  3. Example
  4. Installation
  5. Usage
  6. Browser support
  7. API Documentation
  8. Contributing
  9. License
  10. Big Thanks To

JSON Schema $Ref Parser

This package needs a new maintainer or at least some contributors. For more information please read this article. As of v10.0.0 I am no longer spending any time working on this tool, so I can focus on scaling up my reforestation charity instead of burning myself out trying to maintain a whole load of OSS projects I don't use in my vanishingly small spare time. Get in touch if you'd like to take over. - Phil Sturgeon

Parse, Resolve, and Dereference JSON Schema $ref pointers

Build Status Coverage Status

npm License Buy us a tree

The Problem:

You've got a JSON Schema with $ref pointers to other files and/or URLs. Maybe you know all the referenced files ahead of time. Maybe you don't. Maybe some are local files, and others are remote URLs. Maybe they are a mix of JSON and YAML format. Maybe some of the files contain cross-references to each other.

{
  "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"
    }
  }
}

The Solution:

JSON Schema $Ref Parser is a full JSON Reference and JSON Pointer implementation that crawls even the most complex JSON Schemas and gives you simple, straightforward JavaScript objects.

  • Use JSON or YAML schemas — or even a mix of both!
  • Supports $ref pointers to external files and URLs, as well as custom sources such as databases
  • Can bundle multiple files into a single schema that only has internal $ref pointers
  • Can dereference your schema, producing a plain-old JavaScript object that's easy to work with
  • Supports circular references, nested references, back-references, and cross-references between files
  • Maintains object reference equality — $ref pointers to the same value always resolve to the same object instance
  • Tested in Node v10, v12, & v14, and all major web browsers on Windows, Mac, and Linux

Example

$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);
  }
})

Or use async/await syntax instead. The following example is the same as above:

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

For more detailed examples, please see the API Documentation

Installation

Install using npm:

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

Usage

When using JSON Schema $Ref Parser in Node.js apps, you'll probably want to use CommonJS syntax:

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

When using a transpiler such as Babel or TypeScript, or a bundler such as Webpack or Rollup, you can use ECMAScript modules syntax instead:

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

If you are using Node.js < 18, you'll need a polyfill for fetch, like node-fetch:

import fetch from "node-fetch";

globalThis.fetch = fetch;

Browser support

JSON Schema $Ref Parser supports recent versions of every major web browser. Older browsers may require Babel and/or polyfills.

To use JSON Schema $Ref Parser in a browser, you'll need to use a bundling tool such as Webpack, Rollup, Parcel, or Browserify. Some bundlers may require a bit of configuration, such as setting browser: true in rollup-plugin-resolve.

API Documentation

Full API documentation is available right here

Contributing

I welcome any contributions, enhancements, and bug-fixes. Open an issue on GitHub and submit a pull request.

Building/Testing

To build/test the project locally on your computer:

  1. Clone this repo
    git clone https://github.com/APIDevTools/json-schema-ref-parser.git

  2. Install dependencies
    npm install

  3. Run the tests
    npm test

License

JSON Schema $Ref Parser is 100% free and open-source, under the MIT license. Use it however you want.

This package is Treeware. If you use it in production, then we ask that you buy the world a tree to thank us for our work. By contributing to the Treeware forest you’ll be creating employment for local families and restoring wildlife habitats.

Big Thanks To

Thanks to these awesome companies for their support of Open Source developers ❤

Stoplight SauceLabs Coveralls

版本与发布记录

@bcherny/json-schema-ref-parser 共发布 2 个版本,首次发布于 2022/08/06;过去 12 个月发布了 0 个版本。

版本发布日期距今
10.0.5-forklatest2023/02/233 年前
9.0.92022/08/064 年前

关于 @bcherny/json-schema-ref-parser 的常见问题

@bcherny/json-schema-ref-parser 是什么?

Parse, Resolve, and Dereference JSON Schema $ref pointers

如何安装 @bcherny/json-schema-ref-parser?

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

@bcherny/json-schema-ref-parser 支持 TypeScript 吗?

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

@bcherny/json-schema-ref-parser 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@bcherny/json-schema-ref-parser 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >= 16。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@bcherny/json-schema-ref-parser 可以商用吗?

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

@bcherny/json-schema-ref-parser 的下载量有多少?

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

@bcherny/json-schema-ref-parser 还在维护吗?

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