@cloudflare/json-schema-walker

中文文档
0.1.1发布于 2018/04/21BSD-3-ClauseCommonJS零依赖近 30 天下载 27.7万

"Utilities for walking JSON Schemas and performing actions on them."

原始使用文档
文档目录3 节
  1. The walkers
  2. The callbacks
  3. Vocabularies

JSON Schema Walker

A system that visits all schema objects in a JSON Schema document and makes callbacks before and/or after visiting all of the current schema object's subschemas.

Currently, there are several vocabularies to choose from, which must be used with the two walking functions. As the vocabulary concept is finalized in JSON Schema draft-08, this package will be reworked to support it directly.

The walkers

Both walkers accept one or two callback functions, but differ slightly in their behavior. In both cases, there are "path" or "parentPath" concepts which are arrays of the JSON property names or array indexes followed to reach the current schema.

Note that walker callbacks are expected to modify the schema structure in place, so clone a copy if you need the original as well.

schemaWalk(schema, preFunc, postFunc, vocabulary)

This simply calls the preFunc callback (if there is one), calls subschemaWalk wth an empty parent path of [], and then calls the postFunc callback.

subschemaWalk(scheam, preFunc, postFunc, parentPath, vocabulary)

Sometimes it is useful to start somewhere within a document, which subschemaWalk accomplishes by taking a parent path. This is also how the recursive calls are made once you start a walk- the parent path for each subsequent call is calculated during the walk.

In particular, handling the root schema differently is common, so calling subschemaWalk direclty on the root with an empty parent path [] allows skipping the root schema.

The callbacks

Either the preFunc or postFunc call can be null, or both can be passed for the same walk. They are expected to take the following as positional parameters:

  1. The schema object (or boolean) being processed
  2. The path to the schema from its parent
  3. The parent schema object, if any
  4. The path from the schema document root to the parent

Paths are arrays of property names (strings) and array indices (integers) that are analogous to JSON Pointers. Given ["foo", 42] as the path from the root to the parent object, and ["bar"] as the path from the parent object to the schema being processed, the equivalent JSON Pointer fragment for the parent would be "#/foo/42, while for the schema being processed it would be #/foo/42/bar.

The equivalent Relative JSON Pointer from the parent to the schema being processed would be 0/bar.

An empty path is represented as an empty array []. For a pointer from the root, this is equivalent to # as a JSON Pointer fragment.

For an empty path from the parent to the schema being processed (meaning that both are the same, which happens for the root schema), this is equivalent to 0 as a Relative JSON Pointer.

Vocabularies

getVocabulary(schema, defaultVocabulary) This returns the walker vocabulary object based on the schema's $schema keyword, or returns the default that you pass in if $schema is not present or not recognized. The current default if none is provided is Draft-07 JSON Hyper-Schema.

Once draft-08 is published with a formalized concept of a schema vocabulary, the vocabulary implementation for this package will likely be substantially reworked.

版本与发布记录

@cloudflare/json-schema-walker 共发布 1 个版本,首次发布于 2018/04/21;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.1.1latest2018/04/218 年前

关于 @cloudflare/json-schema-walker 的常见问题

@cloudflare/json-schema-walker 是什么?

"Utilities for walking JSON Schemas and performing actions on them."

如何安装 @cloudflare/json-schema-walker?

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

@cloudflare/json-schema-walker 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/cloudflare__json-schema-walker 是否存在。

@cloudflare/json-schema-walker 支持 ESM 还是 CommonJS?

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

@cloudflare/json-schema-walker 可以商用吗?

@cloudflare/json-schema-walker 使用 BSD-3-Clause 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。另外不得用原作者名义为衍生产品背书。以上为概括,不构成法律意见,请以许可原文为准。

@cloudflare/json-schema-walker 的下载量有多少?

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

@cloudflare/json-schema-walker 还在维护吗?

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