{"name":"@fastify/merge-json-schemas","version":"0.2.0","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","test":"npm run test:unit && npm run test:types","test:unit":"c8 --100 node --test","test:types":"tsd"},"repository":{"type":"git","url":"git+https://github.com/fastify/merge-json-schemas.git"},"keywords":["json","schema","merge","allOf"],"author":{"name":"Ivan Tymoshenko","email":"ivan@tymoshenko.me"},"license":"MIT","bugs":{"url":"https://github.com/fastify/merge-json-schemas/issues"},"homepage":"https://github.com/fastify/merge-json-schemas#readme","devDependencies":{"@fastify/pre-commit":"^2.1.0","c8":"^8.0.1","standard":"^17.1.0","tsd":"^0.30.3"},"dependencies":{"dequal":"^2.0.3"},"_id":"@fastify/merge-json-schemas@0.2.0","gitHead":"11d70798b3a0dc8f0bc5e0f0b6eb9e079e7bbb2c","_nodeVersion":"20.16.0","_npmVersion":"10.8.1","dist":{"integrity":"sha512-9ql2tM7wdn7McvOg1V799G6/M6N5MM/Exp9SQvy8rld3xO07YhpxT9taR6vy7FU8o3ppU6M19M7P6GURP5RsPA==","shasum":"13ebd4f2b57ed3be83421e2e83bc7cbb9cec237d","tarball":"http://registry-npm.ywwl.com/@fastify/merge-json-schemas/download/@fastify/merge-json-schemas-0.2.0.tgz","fileCount":50,"unpackedSize":100831,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCICB0K+D2sKtaHALJNnmiizNHWSaEfHbyogncR7Tkh6sSAiAr/D638dFasGzLEBtlhACyjNT1cBEGEBEI+IFuDlbwuQ=="}],"size":11937,"noattachment":false,"key":"/@fastify/merge-json-schemas/-/@fastify/merge-json-schemas-0.2.0.tgz"},"_npmUser":{"name":"matteo.collina","email":"hello@matteocollina.com"},"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/merge-json-schemas_0.2.0_1725438117363_0.4328192242996358"},"_hasShrinkwrap":false,"readme":"# @fastify/merge-json-schemas\n\n__merge-json-schemas__ 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).\n\n- [Installation](#installation)\n- [Usage](#usage)\n- [API](#api)\n  - [mergeSchemas(schemas, options)](#mergeschemasschemas-options)\n  - [resolvers](#resolvers)\n  - [defaultResolver](#defaultresolver)\n- [License](#license)\n\n<a name=\"installation\"></a>\n\n## Installation\n\n```bash\nnpm install @fastify/merge-json-schemas\n```\n\n<a name=\"usage\"></a>\n\n## Usage\n\n```javascript\nconst assert = require('node:assert')\nconst { mergeSchemas } = require('@fastify/merge-json-schemas');\n\nconst schema1 = {\n  $id: 'schema1',\n  type: 'object',\n  properties: {\n    foo: { type: 'string', enum: ['foo1', 'foo2'] },\n    bar: { type: 'string', minLength: 3 }\n  }\n}\n\nconst schema2 = {\n  $id: 'schema1',\n  type: 'object',\n  properties: {\n    foo: { type: 'string', enum: ['foo1', 'foo3'] },\n    bar: { type: 'string', minLength: 5 }\n  },\n  required: ['foo']\n}\n\nconst mergedSchema = mergeSchemas([schema1, schema2])\nassert.deepStrictEqual(mergedSchema, {\n  $id: 'schema1',\n  type: 'object',\n  properties: {\n    foo: { type: 'string', enum: ['foo1'] },\n    bar: { type: 'string', minLength: 5 }\n  },\n  required: ['foo']\n})\n```\n\n<a name=\"api\"></a>\n\n## API\n\n<a name=\"merge-schemas\"></a>\n\n#### mergeSchemas(schemas, options)\n\nBuilds a logical conjunction (AND) of multiple [JSON schemas](https://json-schema.org/draft/2020-12/json-schema-core#name-introduction).\n\n- `schemas` __\\<objects[]\\>__ - list of JSON schemas to merge.\n- `options` __\\<object\\>__ - optional options.\n  - `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).\n  - `defaultResolver` __\\<function\\>__ - custom default resolver for JSON schema keywords. See [keywordResolver](#keywordresolver-keyword-values-mergedschema-parentschemas-options).\n  - `onConflict` __\\<string\\>__ - action to take when a conflict is found. Used by the default `defaultResolver`. Default is `throw`. Possible values are:\n    - `throw` - throws an error if found a multiple different schemas for the same keyword.\n    - `ignore` - do nothing if found a multiple different schemas for the same keyword.\n    - `first` - use the value of the first schema if found a multiple different schemas for the same keyword.\n\n#### resolvers\n\nA 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).\n\n#### defaultResolver\n\nA 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:\n\n- If only one schema contains the keyword, the value of the keyword is used as the merged value.\n- If multiple schemas contain the exact same value for the keyword, the value of the keyword is used as the merged value.\n- If multiple schemas contain different values for the keyword, it throws an error.\n\n#### keywordResolver (keyword, values, mergedSchema, parentSchemas, options)\n\n__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:\n\n- `keyword` __\\<string\\>__ - the name of the keyword to merge.\n- `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`.\n- `mergedSchema` __\\<object\\>__ - an instance of the merged schema.\n- `parentSchemas` __\\<object[]\\>__ - the list of parent schemas.\n- `options` __\\<object\\>__ - the options passed to `mergeSchemas`.\n\nThe resolver must set the merged value of the `keyword` in the `mergedSchema` object.\n\n__Example:__ resolver for the `minNumber` keyword.\n\n```javascript\nfunction minNumberResolver (keyword, values, mergedSchema) {\n  mergedSchema[keyword] = Math.min(...values)\n}\n```\n\n<a name=\"license\"></a>\n\n## License\n\nMIT\n","publish_time":1725438117492,"maintainers":[{"name":"gurgunday","email":"hey@gurgun.day"},{"name":"metcoder95","email":"me@metcoder.dev"},{"name":"galvez","email":"jonasgalvez@gmail.com"},{"name":"simenb","email":"sbekkhus91@gmail.com"},{"name":"coopflow","email":"community@coopflow.com"},{"name":"simoneb","email":"simone.busoli@gmail.com"},{"name":"rafaelgss","email":"rafael.nunu@hotmail.com"},{"name":"starptech","email":"deusdustin@gmail.com"},{"name":"delvedor","email":"tommydelved@gmail.com"},{"name":"matteo.collina","email":"hello@matteocollina.com"},{"name":"allevo","email":"tomallevi@gmail.com"},{"name":"jsumners","email":"james.sumners@gmail.com"},{"name":"zekth","email":"vince.legoff@gmail.com"},{"name":"eomm","email":"behemoth89@gmail.com"},{"name":"fox1t","email":"maksim@sinik.it"},{"name":"airhorns","email":"harry@harry.me"},{"name":"kibertoad","email":"iselwin@gmail.com"},{"name":"climba03003","email":"kaka@kakawebsitedemo.com"},{"name":"ivan-tymoshenko","email":"ivan@tymoshenko.me"}],"_cnpm_publish_time":1725438117492,"dist-tags":{"latest":"0.2.2"}}