# @fastify/merge-json-schemas

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

Latest version **0.2.1** (published 2025-01-06) · MIT license · 0 weekly downloads

## Install

```sh
npm install @fastify/merge-json-schemas
pnpm add @fastify/merge-json-schemas
yarn add @fastify/merge-json-schemas
bun add @fastify/merge-json-schemas
```

## Health

**Score 45/100 (D)** — status: stable.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support; pre 1.0.

Negative: stale.

## Facts

| | |
|---|---|
| Version | 0.2.1 |
| Published | 2025-01-06 |
| First published | 2024-01-16 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 99.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 12 |
| Author | Ivan Tymoshenko |
| Maintainers | starptech, simoneb, coopflow, delvedor, matteo.collina, allevo, jsumners, zekth, rafaelgss, eomm, fox1t, airhorns, kibertoad, climba03003, galvez, simenb, gurgunday, metcoder95, ivan-tymoshenko, fdawgs |
| Keywords | json, schema, merge, allOf |

## Links

- npm: https://www.npmjs.com/package/@fastify/merge-json-schemas
- Repository: https://github.com/fastify/merge-json-schemas
- Homepage: https://github.com/fastify/merge-json-schemas#readme
- Issues: https://github.com/fastify/merge-json-schemas/issues
- Funding: https://github.com/sponsors/fastify
- npm.io page: https://npm.io/package/@fastify/merge-json-schemas

## Dependencies (1)

- [dequal](https://npm.io/package/dequal.md) ^2.0.3

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 0.2.1 (latest) — 2025-01-06
- 0.2.0 — 2024-09-04
- 0.1.1 — 2024-01-16
- 0.1.0 — 2024-01-16

## README

# @fastify/merge-json-schemas

[![CI](https://github.com/fastify/merge-json-schemas/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/fastify/merge-json-schemas/actions/workflows/ci.yml)
[![npm version](https://img.shields.io/npm/v/@fastify/merge-json-schemas)](https://www.npmjs.com/package/@fastify/merge-json-schemas)
[![neostandard javascript style](https://img.shields.io/badge/code_style-neostandard-brightgreen?style=flat)](https://github.com/neostandard/neostandard)

__merge-json-schemas__ is a JavaScript library that builds a logical product (AND) for multiple [JSON schemas](https://json-schema.org/draft/2020-12/json-schema-core#name-introduction).

- [Installation](#installation)
- [Usage](#usage)
- [API](#api)
  - [mergeSchemas(schemas, options)](#mergeschemasschemas-options)
  - [resolvers](#resolvers)
  - [defaultResolver](#defaultresolver)
- [License](#license)

<a name="installation"></a>

## Installation

```bash
npm i @fastify/merge-json-schemas
```

<a name="usage"></a>

## Usage

```javascript
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']
})
```

<a name="api"></a>

## API

<a name="merge-schemas"></a>

#### mergeSchemas(schemas, options)

Builds a logical conjunction (AND) of multiple [JSON schemas](https://json-schema.org/draft/2020-12/json-schema-core#name-introduction).

- `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](#keywordresolver-keyword-values-mergedschema-parentschemas-options)
  - `defaultResolver` __\<function\>__ - custom default resolver for JSON schema keywords. See [keywordResolver](#keywordresolver-keyword-values-mergedschema-parentschemas-options)
  - `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](#keywordresolver-keyword-values-mergedschema-parentschemas-options).

#### 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.

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

<a name="license"></a>

## License

Licensed under [MIT](./LICENSE).

---
_Source: https://npm.io/package/@fastify/merge-json-schemas · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
