Package Exports
- @fastify/merge-json-schemas
- @fastify/merge-json-schemas/index.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@fastify/merge-json-schemas) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
@fastify/merge-json-schemas
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 mergeoptions
<object> - optional optionsresolvers
<object> - custom resolvers for JSON schema keywords. Each key is the name of a JSON schema keyword. Each value is a resolver function. See keywordResolverdefaultResolver
<function> - custom default resolver for JSON schema keywords. See keywordResolveronConflict
<string> - action to take when a conflict is found. Used by the defaultdefaultResolver
. Default isthrow
. Possible values are:throw
- throws an error multiple different schemas for the same keyword are foundignore
- do nothing if multiple different schemas for the same keyword are foundfirst
- 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 mergevalues
<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 isundefined
mergedSchema
<object> - an instance of the merged schemaparentSchemas
<object[]> - the list of parent schemasoptions
<object> - the options passed tomergeSchemas
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.