@fastify/merge-json-schemas

中文文档
0.2.2发布于 2026/10/10MITTypeScriptCommonJS构建来源已验证近 30 天下载 8017.2万

Builds a logical conjunction (AND) of multiple JSON schemas

原始使用文档
文档目录4 节
  1. Installation
  2. Usage
  3. API
  4. License

@fastify/merge-json-schemas

CI npm version neostandard javascript style

merge-json-schemas is a JavaScript library that builds a logical product (AND) for multiple JSON schemas.

Installation

npm i @fastify/merge-json-schemas

Usage

const assert = require('node:assert')
const { mergeSchemas } = require('@fastify/merge-json-schemas');

const schema1 = {
  $id: 'schema1',
  type: 'object',
  properties: {
    foo: { type: 'string', enum: ['foo1', 'foo2'] },
    bar: { type: 'string', minLength: 3 }
  }
}

const schema2 = {
  $id: 'schema1',
  type: 'object',
  properties: {
    foo: { type: 'string', enum: ['foo1', 'foo3'] },
    bar: { type: 'string', minLength: 5 }
  },
  required: ['foo']
}

const mergedSchema = mergeSchemas([schema1, schema2])
assert.deepStrictEqual(mergedSchema, {
  $id: 'schema1',
  type: 'object',
  properties: {
    foo: { type: 'string', enum: ['foo1'] },
    bar: { type: 'string', minLength: 5 }
  },
  required: ['foo']
})

API

mergeSchemas(schemas, options)

Builds a logical conjunction (AND) of multiple JSON schemas.

  • schemas <objects[]> - list of JSON schemas to merge
  • options <object> - optional options
    • resolvers <object> - custom resolvers for JSON schema keywords. Each key is the name of a JSON schema keyword. Each value is a resolver function. See keywordResolver
    • defaultResolver <function> - custom default resolver for JSON schema keywords. See keywordResolver
    • onConflict <string> - action to take when a conflict is found. Used by the default defaultResolver. Default is throw. Possible values are:
      • throw - throws an error multiple different schemas for the same keyword are found
      • ignore - do nothing if multiple different schemas for the same keyword are found
      • first - use the value of the first schema if multiple different schemas for the same keyword are found

resolvers

A list of default resolvers that merge-json-schema uses to merge JSON schemas. You can override the default resolvers by passing a list of custom resolvers in the options argument of mergeSchemas. See keywordResolver.

defaultResolver

A default resolver that merge-json-schema uses to merge JSON schemas. Default resolver is used when no custom resolver is defined for a JSON schema keyword. By default, the default resolver works as follows:

  • If only one schema contains the keyword, the value of the keyword is used as the merged value
  • If multiple schemas contain the exact same value for the keyword, the value of the keyword is used as the merged value
  • If multiple schemas contain different values for the keyword, it throws an error

keywordResolver (keyword, values, mergedSchema, parentSchemas, options)

merge-json-schema uses a set of resolvers to merge JSON schemas. Each resolver is associated with a JSON schema keyword. The resolver is called when the keyword is found in the schemas to merge. The resolver is called with the following arguments:

  • keyword <string> - the name of the keyword to merge
  • values <any[]> - the values of the keyword to merge. The length of the array is equal to the number of schemas to merge. If a schema does not contain the keyword, the value is undefined
  • mergedSchema <object> - an instance of the merged schema
  • parentSchemas <object[]> - the list of parent schemas
  • options <object> - the options passed to mergeSchemas

The resolver must set the merged value of the keyword in the mergedSchema object.

Example: resolver for the minNumber keyword.

function minNumberResolver (keyword, values, mergedSchema) {
  mergedSchema[keyword] = Math.min(...values)
}

License

Licensed under MIT.

版本与发布记录

@fastify/merge-json-schemas 共发布 5 个版本,首次发布于 2024/01/16;过去 12 个月发布了 1 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.2.2latest2026/10/101 个月前
0.2.12025/01/071.9 年前
0.2.02024/09/042 年前
0.1.12024/01/162 年前
0.1.02024/01/162 年前

关于 @fastify/merge-json-schemas 的常见问题

@fastify/merge-json-schemas 是什么?

Builds a logical conjunction (AND) of multiple JSON schemas

如何安装 @fastify/merge-json-schemas?

在项目目录运行 npm install @fastify/merge-json-schemas,或使用 pnpm add / yarn add / bun add。

@fastify/merge-json-schemas 支持 TypeScript 吗?

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

@fastify/merge-json-schemas 支持 ESM 还是 CommonJS?

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

@fastify/merge-json-schemas 可以商用吗?

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

@fastify/merge-json-schemas 的下载量有多少?

根据 npm 官方下载统计,@fastify/merge-json-schemas 在 2026/10/15 – 2026/11/13 这 30 天内被下载 80,172,216 次,日均约 2,672,407 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@fastify/merge-json-schemas 还在维护吗?

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