npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@fastify/merge-json-schemas

v0.2.0

Published

Builds a logical conjunction (AND) of multiple JSON schemas

Downloads

6,026,938

Readme

@fastify/merge-json-schemas

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

Installation

npm install @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 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.

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

MIT