Aktueller Stand

This commit is contained in:
2026-01-22 19:05:45 +01:00
parent 85dee61a4d
commit e280e4eadb
1967 changed files with 397327 additions and 74093 deletions

View File

@@ -0,0 +1,13 @@
version: 2
updates:
- package-ecosystem: "github-actions"
directory: "/"
schedule:
interval: "monthly"
open-pull-requests-limit: 10
- package-ecosystem: "npm"
directory: "/"
schedule:
interval: "weekly"
open-pull-requests-limit: 10

View File

@@ -1,32 +0,0 @@
name: Run Tests
on: [push, pull_request]
permissions:
contents: read
jobs:
test:
name: Test
runs-on: ${{ matrix.os }}
strategy:
matrix:
node-version: [18.x, 20.x]
os: [ubuntu-latest, windows-latest, macOS-latest]
steps:
- uses: actions/checkout@v3
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
- name: Install
run: |
npm install
- name: Test
run: |
npm run test

View File

@@ -0,0 +1,23 @@
name: CI
on:
push:
branches:
- main
- master
- next
- 'v*'
paths-ignore:
- 'docs/**'
- '*.md'
pull_request:
paths-ignore:
- 'docs/**'
- '*.md'
jobs:
test:
uses: fastify/workflows/.github/workflows/plugins-ci.yml@v5
with:
license-check: true
lint: true

View File

@@ -1,6 +1,10 @@
# @fastify/merge-json-schema
# @fastify/merge-json-schemas
__merge-json-schema__ is a javascript library that build a logical product (AND) for multiple [JSON schemas](https://json-schema.org/draft/2020-12/json-schema-core#name-introduction).
[![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)
@@ -15,7 +19,7 @@ __merge-json-schema__ is a javascript library that build a logical product (AND)
## Installation
```bash
npm install @fastify/merge-json-schema
npm i @fastify/merge-json-schemas
```
<a name="usage"></a>
@@ -24,7 +28,7 @@ npm install @fastify/merge-json-schema
```javascript
const assert = require('node:assert')
const { mergeSchemas } = require('merge-json-schema')
const { mergeSchemas } = require('@fastify/merge-json-schemas');
const schema1 = {
$id: 'schema1',
@@ -67,14 +71,14 @@ assert.deepStrictEqual(mergedSchema, {
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).
- `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 if found a multiple different schemas for the same keyword.
- `ignore` - do nothing if found a multiple different schemas for the same keyword.
- `first` - use the value of the first schema if found a multiple different schemas for the same keyword.
- `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
@@ -84,19 +88,19 @@ A list of default resolvers that __merge-json-schema__ uses to merge JSON schema
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.
- 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`.
- `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.
@@ -112,4 +116,4 @@ function minNumberResolver (keyword, values, mergedSchema) {
## License
MIT
Licensed under [MIT](./LICENSE).

View File

@@ -0,0 +1,6 @@
'use strict'
module.exports = require('neostandard')({
ignores: require('neostandard').resolveIgnoresFromGitignore(),
ts: true
})

View File

@@ -1,6 +1,6 @@
'use strict'
const deepEqual = require('fast-deep-equal')
const { dequal: deepEqual } = require('dequal')
const resolvers = require('./lib/resolvers')
const errors = require('./lib/errors')
@@ -46,7 +46,7 @@ const keywordsResolvers = {
contains: mergeSchemasResolver
}
function mergeSchemasResolver (keyword, values, mergedSchema, schemas, options) {
function mergeSchemasResolver (keyword, values, mergedSchema, _schemas, options) {
mergedSchema[keyword] = _mergeSchemas(values, options)
}
@@ -66,7 +66,7 @@ function cartesianProduct (arrays) {
return result
}
function mergeOneOf (keyword, values, mergedSchema, schemas, options) {
function mergeOneOf (keyword, values, mergedSchema, _schemas, options) {
if (values.length === 1) {
mergedSchema[keyword] = values[0]
return
@@ -181,7 +181,7 @@ function getSchemaForProperty (schema, propertyName) {
return additionalProperties
}
function mergeProperties (keyword, values, mergedSchema, schemas, options) {
function mergeProperties (keyword, _values, mergedSchema, schemas, options) {
const foundProperties = {}
for (const currentSchema of schemas) {
const properties = currentSchema.properties ?? {}
@@ -210,7 +210,7 @@ function mergeProperties (keyword, values, mergedSchema, schemas, options) {
mergedSchema[keyword] = mergedProperties
}
function mergeObjects (keyword, values, mergedSchema, schemas, options) {
function mergeObjects (keyword, values, mergedSchema, _schemas, options) {
const objectsProperties = {}
for (const properties of values) {
@@ -232,7 +232,7 @@ function mergeObjects (keyword, values, mergedSchema, schemas, options) {
mergedSchema[keyword] = mergedProperties
}
function mergeIfThenElseSchemas (keyword, values, mergedSchema, schemas, options) {
function mergeIfThenElseSchemas (_keyword, _values, mergedSchema, schemas, options) {
for (let i = 0; i < schemas.length; i++) {
const subSchema = {
if: schemas[i].if,
@@ -313,7 +313,7 @@ function _mergeSchemas (schemas, options) {
return mergedSchema
}
function defaultResolver (keyword, values, mergedSchema, schemas, options) {
function defaultResolver (keyword, values, mergedSchema, _schemas, options) {
const onConflict = options.onConflict ?? 'throw'
if (values.length === 1 || onConflict === 'first') {

View File

@@ -1,6 +1,6 @@
'use strict'
const deepEqual = require('fast-deep-equal')
const { dequal: deepEqual } = require('dequal')
const { MergeError } = require('./errors')
function _arraysIntersection (arrays) {

View File

@@ -1,16 +1,16 @@
{
"name": "@fastify/merge-json-schemas",
"version": "0.1.1",
"version": "0.2.1",
"description": "Builds a logical conjunction (AND) of multiple JSON schemas",
"main": "index.js",
"type": "commonjs",
"types": "types/index.d.ts",
"scripts": {
"lint": "standard",
"lint:fix": "standard --fix",
"lint": "eslint",
"lint:fix": "eslint --fix",
"test": "npm run test:unit && npm run test:types",
"test:unit": "c8 --100 node --test",
"test:types": "tsd",
"test": "npm run lint && npm run test:unit && npm run test:types"
"test:types": "tsd"
},
"repository": {
"type": "git",
@@ -23,17 +23,45 @@
"allOf"
],
"author": "Ivan Tymoshenko <ivan@tymoshenko.me>",
"contributors": [
{
"name": "Matteo Collina",
"email": "hello@matteocollina.com"
},
{
"name": "Frazer Smith",
"email": "frazer.dev@icloud.com",
"url": "https://github.com/fdawgs"
},
{
"name": "Gürgün Dayıoğlu",
"email": "hey@gurgun.day",
"url": "https://heyhey.to/G"
}
],
"license": "MIT",
"bugs": {
"url": "https://github.com/fastify/merge-json-schemas/issues"
},
"homepage": "https://github.com/fastify/merge-json-schemas#readme",
"funding": [
{
"type": "github",
"url": "https://github.com/sponsors/fastify"
},
{
"type": "opencollective",
"url": "https://opencollective.com/fastify"
}
],
"devDependencies": {
"c8": "^8.0.1",
"standard": "^17.1.0",
"tsd": "^0.30.3"
"@fastify/pre-commit": "^2.1.0",
"c8": "^10.1.3",
"eslint": "^9.17.0",
"neostandard": "^0.12.0",
"tsd": "^0.31.2"
},
"dependencies": {
"fast-deep-equal": "^3.1.3"
"dequal": "^2.0.3"
}
}

View File

@@ -52,11 +52,10 @@ export type MergeOptions = {
defaultResolver?: KeywordResolver,
resolvers?: Partial<KeywordResolvers>,
// enum of ["throw", "skip", "first"]
onConflict?: "throw" | "skip" | "first"
onConflict?: 'throw' | 'skip' | 'first'
}
export function mergeSchemas(schemas: any[], options?: MergeOptions): any;
export function mergeSchemas (schemas: any[], options?: MergeOptions): any
export const keywordsResolvers: KeywordResolvers
export const defaultResolver: KeywordResolver