
Product
A New Overview in our Dashboard
We redesigned Socket's first logged-in page to display rich and insightful visualizations about your repositories protected against supply chain threats.
@fastify/merge-json-schemas
Advanced tools
Builds a logical conjunction (AND) of multiple JSON schemas
@fastify/merge-json-schemas is an npm package designed to merge multiple JSON schemas into a single schema. This is particularly useful in Fastify applications where you might have multiple schemas for different parts of your application and you want to combine them for validation or other purposes.
Basic Schema Merging
This feature allows you to merge two or more JSON schemas into a single schema. The resulting schema will include all properties from the input schemas.
{"const merge = require('@fastify/merge-json-schemas');":"","const schema1 = {":""," type: 'object',":""," properties: {":""," name: { type: 'string' }":""," }":"","};":"","const schema2 = {":""," age: { type: 'number' }":"","const mergedSchema = merge([schema1, schema2]);":"","console.log(mergedSchema);":""}
Nested Schema Merging
This feature allows you to merge nested JSON schemas. The resulting schema will include nested properties from all input schemas.
{"const merge = require('@fastify/merge-json-schemas');":"","const schema1 = {":""," type: 'object',":""," properties: {":""," address: {":""," type: 'object',":""," properties: {":""," street: { type: 'string' }":""," }":""," }":""," }":"","};":"","const schema2 = {":""," city: { type: 'string' }":"","const mergedSchema = merge([schema1, schema2]);":"","console.log(mergedSchema);":""}
json-schema-merge-allof is a package that merges JSON schemas using the 'allOf' keyword. It is useful for combining schemas that use the 'allOf' keyword to represent a combination of multiple schemas. Compared to @fastify/merge-json-schemas, it focuses more on the 'allOf' keyword and may not be as flexible for other types of schema merging.
json-schema-ref-parser is a powerful tool for dereferencing and bundling JSON schemas. It can resolve $ref pointers in your schemas and combine them into a single schema. While it offers more advanced features like dereferencing, it may be more complex to use for simple schema merging compared to @fastify/merge-json-schemas.
deepmerge is a general-purpose library for deep merging of JavaScript objects. It can be used to merge JSON schemas, but it is not specifically designed for this purpose. Compared to @fastify/merge-json-schemas, deepmerge is more versatile but may require additional handling to properly merge JSON schemas.
merge-json-schemas is a JavaScript library that builds a logical product (AND) for multiple JSON schemas.
npm i @fastify/merge-json-schemas
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']
})
Builds a logical conjunction (AND) of multiple JSON schemas.
schemas
<objects[]> - list of JSON schemas to mergeoptions
<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 keywordResolverdefaultResolver
<function> - custom default resolver for JSON schema keywords. See keywordResolveronConflict
<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 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 foundA 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.
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:
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 is undefined
mergedSchema
<object> - an instance of the merged schemaparentSchemas
<object[]> - the list of parent schemasoptions
<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)
}
Licensed under MIT.
FAQs
Builds a logical conjunction (AND) of multiple JSON schemas
The npm package @fastify/merge-json-schemas receives a total of 1,227,321 weekly downloads. As such, @fastify/merge-json-schemas popularity was classified as popular.
We found that @fastify/merge-json-schemas demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 20 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
We redesigned Socket's first logged-in page to display rich and insightful visualizations about your repositories protected against supply chain threats.
Product
Automatically fix and test dependency updates with socket fix—a new CLI tool that turns CVE alerts into safe, automated upgrades.
Security News
CISA denies CVE funding issues amid backlash over a new CVE foundation formed by board members, raising concerns about transparency and program governance.