json-schema-intersect
v0.1.0
Published
Combine multiple JSON Schemas into a single schema which matches instances which are valid for all of the combined schemas, for supporting older JSON Schema versions which lack support for allOf, such Draft 4 used by Swagger/OpenAPI 2.
Readme
JSON Schema Intersect
A library to combine multiple JSON Schemas into a
single schema which matches instances which are valid for all of the combined
schemas. This is useful for supporting older JSON Schema versions which lack
support for
allOf,
such as JSON Schema Draft
4 used by
Swagger/OpenAPI 2.
The combined schema behaves as closely as possible to a schema with
allOf.
containing each of the input schemas.
[!IMPORTANT]
Use allOf
to create a combined schema when possible.
Introductory Example
import assert from 'node:assert/strict';
import jsonSchemaIntersect from 'json-schema-intersect';
assert.deepEqual(
jsonSchemaIntersect(
{
type: 'number',
minimum: 5,
maximum: 15,
},
{
type: 'number',
minimum: 0,
maximum: 10,
},
),
{
type: 'number',
minimum: 5,
maximum: 10,
},
);Features
- Supports Schema objects from
Swagger/OpenAPI 2
and OpenAPI 3, including
nullable,readOnly,writeOnly, and other constraints.
Installation
This package can be installed using npm, either globally or locally, by running:
npm install json-schema-intersectRecipes
More examples can be found in the test specifications.
API Docs
To use this module as a library, see the API Documentation.
Contributing
Contributions are appreciated. Contributors agree to abide by the Contributor Covenant Code of Conduct. If this is your first time contributing to a Free and Open Source Software project, consider reading How to Contribute to Open Source in the Open Source Guides.
If the desired change is large, complex, backwards-incompatible, can have significantly differing implementations, or may not be in scope for this project, opening an issue before writing the code can avoid frustration and save a lot of time and effort.
License
This project is available under the terms of the MIT License. See the summary at TLDRLegal.
